ccakephp2.9 複合 Behavior of saveAll when setting compound unique key

Asked 2 months ago, Updated 2 months ago, 1 views

The table structure is as follows:

CREATE TABLE `products`(
`uid`int(11) NOT NULL,
`item_code`varchar(255) NOT NULL,
`stock`int(11) NOT NULL,
`modified`datetime NOT NULL,
`created`datetime NOT NULL,
UNIQUE KEY `unique_keys`(`uid`, `item_code`)

with uid:5, item_code:b-test records present

    (int) 0 = > array(
        'item_code' = > 'b-test',
        'uid' = > '5'
    (int) 1 = > array(
        'item_code' = > 'w-test',
        'uid' = > '5'


If you execute the above code for this table,

Integrity constraint violation: 1062 Duplicate entry '5-b-test' for key 'unique_keys'

I was scolded and it didn't update.
Can't you save and update the composite unique key with cakephp?

mysql cakephp

2022-09-30 11:33

1 Answers

CakePHP 2.x series does not support compound principal keys.

Rewriting parts of the Model class that depend on the primaryKey may not be unacceptable, but it is not recommended because of the large side effects.

The best solution is to add a surrogate key to the table, but if not, create a separate method to save the composite primary key to the table, such as updateAll.

In addition, CakePHP 3.x series supports composite main keys.

2022-09-30 11:33

If you have any answers or tips

© 2022 OneMinuteCode. All rights reserved.