[英]Laravel doesn't accept $primaryKey
Laravel 4 is not accepting my definition of $primaryKey
Laravel 4不接受我对$primaryKey
定义
My Model looks like 我的模特看起来像
class Email extends Eloquent
{
protected $table = 'emails';
protected $primaryKey = 'email';
}
Controller looks like: 控制器看起来像:
$user = new User();
$user->name = Input::get('name');
$email = new Email();
$email->email = Input::get('email');
$user->emails()->save($email);
$user->save();
Database tells me 数据库告诉我
Column not found: 1054 Unknown column 'id' in 'field list' (SQL: insert into
emails
(id
,updated_at
,created_at
) values (myemail@me.com, , 2014-10-04 11:12:25, 2014-10-04 11:12:25)) 列未找到:1054在'字段列表'未知列'ID'(SQL:插入到emails
(id
,updated_at
,created_at
)值(myemail@me.com,2014年10月4日11点12分25秒, 2014-10-04 11:12:25))
Database schema 数据库架构
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`)
);
Relations 关系
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');
}
...
}
How can I get rid of the id column in the insert query? 如何摆脱插入查询中的id列?
I think you should first change the order of saving to: 我认为您应该先将保存顺序更改为:
$user = new User();
$user->name = Input::get('name');
$user->save();
$email = new Email();
$email->email = Input::get('email');
$user->emails()->save($email);
EDIT 编辑
The problem is your relation: 问题是您的关系:
return $this->hasMany('Email', 'id', 'user_id');
it should be: 它应该是:
return $this->hasMany('Email', 'user_id', 'id');
assuming in User
table you have column id
假设在User
表中您具有列id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.