如何在Cakephp3复合主键中插入带空值的记录?

时间:2016-05-20 04:44:46

标签: php mysql cakephp-3.0

我的received_po_details表有3个主键receive_po_id,product_id,product_serial。 有时,product_serial有价值,有时没有

如果product_serial为空,

cakephp3不允许我插入记录

错误

  

无法插入行,缺少部分主键值。拿到   (12,1,),期待(received_po_id,product_id,product_serial)

sql table

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只是一个值。它不是外键

entity - 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] => 
                )

        )

)

感谢

1 个答案:

答案 0 :(得分:0)

我认为你应该将product_serial的默认值设置为null 它应该是这样的..

  `product_serial` varchar(255) DEFAULT NULL,