0
我的received_po_details表有3個主鍵received_po_id,product_id,product_serial。 有時product_serial具有價值,有時什麼也沒有如何在Cakephp3複合主鍵中插入帶空值的記錄?
cakephp3是不是讓我插入記錄如果product_serial是空
錯誤
無法插入行,一些主鍵值都失蹤。有 (12,1,),希望(received_po_id,PRODUCT_ID,product_serial)
SQL表
CREATE TABLE IF NOT EXISTS `received_po_details` (
`received_po_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`quantity_received` int(11) NOT NULL,
`product_serial` varchar(255),
PRIMARY KEY (received_po_id, product_id, product_serial),
FOREIGN KEY received_po_key (received_po_id) REFERENCES received_pos(id),
FOREIGN KEY product_key (product_id) REFERENCES products(id),
UNIQUE KEY received_po_detail_key (received_po_id, product_id, product_serial)
);
注意product_serial只是一個值。它不是一個外鍵
實體 - receivedPoDetail.php
protected $_accessible = [
'*' => true,
'received_po_id' => true,
'product_id' => true,
'product_serial' => true,
];
控制器
$receivedPo = $this->ReceivedPos->patchEntity($receivedPo, $this->request->data);
請求數據打印
Array
(
[purchase_order_id] => 1
[depot_id] => 1
[prepared_by_id] =>
[approved_by_id] =>
[date_registration] => Array
(
[year] => 2016
[month] => 05
[day] => 20
[hour] => 03
[minute] => 45
)
[comment] =>
[received_po_status_id] => 2
[received_po_details] => Array
(
[0] => Array
(
[product_id] => 1
[quantity_received] => 100
[product_serial] =>
)
[1] => Array
(
[product_id] => 2
[quantity_received] => 1
[product_serial] => random_serial_here
)
[2] => Array
(
[product_id] => 3
[quantity_received] => 300
[product_serial] =>
)
)
)
感謝
它現在從$這個 - 編輯我ReceivedPoDetailsTable初始化方法的PrimaryKey工作>的PrimaryKey([ 'received_po_id', '的product_id', 'product_serial' ]); to $ this-> primaryKey(['received_po_id','product_id']);和它現在的工作。還在buildRules方法中添加了$ rules-> add($ rules-> isUnique(['received_po_id','product_id','product_serial']))。 但idk,如果這是這個正確的解決方案。 – Michael