我在使用 Laravel 时从来没有遇到过任何问题,但我遇到了一些非常奇怪的事情。
我有一个one to one
将用户元数据存储在单独表中的关系。
我的控制器更新看起来像这样
$val = Validator::make(Input::all(), $rules);
if($val->passes())
{
$this->cur_user->username = Input::get('username');
$this->cur_user->metadata->first_name = Input::get('first_name');
$this->cur_user->metadata->last_name = Input::get('last_name');
$this->cur_user->metadata->gender = Input::get('gender');
$this->cur_user->metadata->location = Input::get('location');
$this->cur_user->email = Input::get('email');
$this->cur_user->metadata->dob = Input::get('dob');
$this->cur_user->metadata->about = Input::get('about');
if ($this->cur_user->save() && $this->cur_user->metadata->save())
{
$data = array('msg' => 'success');
}
关系
用户模型
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
public function metadata()
{
return $this->hasOne('metadata');
}
}
元数据模型
class Metadata extends Eloquent {
public $timestamps = false;
protected $table = "users_metadata";
public function user()
{
return $this->belongsTo('user');
}
}
真正奇怪的是,如果我离开 $this->cur_user->metadata->gender = Input::get('gender');
并且$this->cur_user->metadata->about = Input::get('about');
在我的验证中我得到以下错误
{"error":{"type":"Exception","message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update `users_metadata` set `gender` = ?, `about` = ? where `id` is null) (Bindings: array (\n 0 => '1',\n 1 => 'testing one 2 3',\n))","file":"C:\\BitNami\\apache2\\htdocs\\laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php","line":555}}
如果我将其注释掉,一切都很好并且运行良好,检查了所有内容,我没有看到任何奇怪的事情。
请有人给我一个提示吗?(四处搜索找到了一些答案,但不是真的针对我的问题)