表的結(jié)構(gòu)如下:
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()方法,他會(huì)根據(jù)primaryKey來保存數(shù)據(jù),請(qǐng)問如何在Model設(shè)置這種復(fù)合主鍵?
/** * 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'
表的結(jié)構(gòu)如下:
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()方法,他會(huì)根據(jù)primaryKey來保存數(shù)據(jù),請(qǐng)問如何在Model設(shè)置這種復(fù)合主鍵?
/** * 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'
這個(gè)問題沒有解決,大部分別的框架也沒有解決,所以暫時(shí)沒有辦法在Eloquent ORM 定義復(fù)合主鍵,解決辦法:
1.使用doctrine。
2.在表單驗(yàn)證里面做驗(yàn)證,也能達(dá)到主鍵約束的效果。
關(guān)于這個(gè)問題其實(shí)在一年前就有國外的程序員在github上提了issue的,并且taylorotwell還參與了討論,他給出的回復(fù)是laravel目前沒有計(jì)劃增加這個(gè)功能。直到今天(2015.11.07)都沒有這個(gè)功能。詳情請(qǐng)見以下鏈接:https://github.com/laravel/framework/issues/5355
每個(gè)人都需要一臺(tái)速度更快、更穩(wěn)定的 PC。隨著時(shí)間的推移,垃圾文件、舊注冊(cè)表數(shù)據(jù)和不必要的后臺(tái)進(jìn)程會(huì)占用資源并降低性能。幸運(yùn)的是,許多工具可以讓 Windows 保持平穩(wěn)運(yùn)行。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)