Laravel 4不接受我对$primaryKey
我的模型看起来像
class Email extends Eloquent
{
protected $table = 'emails';
protected $primaryKey = 'email';
}
控制器看起来像:
$user = new User();
$user->name = Input::get('name');
$email = new Email();
$email->email = Input::get('email');
$user->emails()->save($email);
$user->save();
数据库告诉我
未找到列:1054未知列' id'在'字段列表' (SQL:插入
emails
(id
,updated_at
,created_at
) 价值观(myemail@me.com ,, 2014-10-04 11:12:25,2014-10-04 11:12:25))
数据库架构
create table `emails` (
`email` varchar(255),
`confirmed` char(1) not null default 'N',
`user_id` bigint(20),
`created_at` timestamp not null default '0000-00-00 00:00:00',
`updated_at` timestamp not null default '0000-00-00 00:00:00',
primary key (`email`)
);
关系
class Email extends Eloquent {
...
public function user()
{
return $this->belongsTo('User', 'user_id');
}
...
}
class User extends Eloquent {
...
public function emails()
{
return $this->hasMany('Email', 'id', 'user_id');
}
...
}
如何摆脱插入查询中的id列?
答案 0 :(得分:1)
我认为您应该首先将保存顺序更改为:
$user = new User();
$user->name = Input::get('name');
$user->save();
$email = new Email();
$email->email = Input::get('email');
$user->emails()->save($email);
修改强>
问题在于你的关系:
return $this->hasMany('Email', 'id', 'user_id');
它应该是:
return $this->hasMany('Email', 'user_id', 'id');
在User
表中假设您有列id