您好,欢迎访问一九零五行业门户网

Laravel怎么在Model定义复合主键

表的结构如下:
reate table `carts` ( `user_id` int(10) unsigned not null, `product_id` char(64) not null, `quantity` int(10) unsigned not null, `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 (`user_id`,`product_id`), key `fk_idx_cart_product` (`product_id`), constraint `fk_idx_cart_product` foreign key (`product_id`) references `products` (`id`), constraint `fk_idx_cart_user` foreign key (`user_id`) references `users` (`id`)) engine=innodb default charset=utf8

需要用eloquent orm的save()方法,他会根据primarykey来保存数据,请问如何在model设置这种复合主键?
/** * the primary key for the model. * * @var string */protected $primarykey = 'id';

$cart = cart::firstornew([...]);$cart->quantity = $quantity;$cart->save();

sqlstate[42s22]: column not found: 1054 unknown column 'id' in 'where clause'
回复内容: 表的结构如下:
reate table `carts` ( `user_id` int(10) unsigned not null, `product_id` char(64) not null, `quantity` int(10) unsigned not null, `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 (`user_id`,`product_id`), key `fk_idx_cart_product` (`product_id`), constraint `fk_idx_cart_product` foreign key (`product_id`) references `products` (`id`), constraint `fk_idx_cart_user` foreign key (`user_id`) references `users` (`id`)) engine=innodb default charset=utf8

需要用eloquent orm的save()方法,他会根据primarykey来保存数据,请问如何在model设置这种复合主键?
/** * the primary key for the model. * * @var string */protected $primarykey = 'id';

$cart = cart::firstornew([...]);$cart->quantity = $quantity;$cart->save();

sqlstate[42s22]: column not found: 1054 unknown column 'id' in 'where clause'
这个问题没有解决,大部分别的框架也没有解决,所以暂时没有办法在eloquent orm 定义复合主键,解决办法:
1.使用doctrine。
2.在表单验证里面做验证,也能达到主键约束的效果。
关于这个问题其实在一年前就有国外的程序员在github上提了issue的,并且taylorotwell还参与了讨论,他给出的回复是laravel目前没有计划增加这个功能。直到今天(2015.11.07)都没有这个功能。详情请见以下链接:https://github.com/laravel/framework/issues/5355
其它类似信息

推荐信息