Limitations of Zend_Db_Table::save on missing primary key
0 reputation · 17 Apr 2024, 13:20 UTC
0 reputation · 17 Apr 2024, 13:20 UTC
Determine how Zend Framework 2’s Zend_Db_Table::save reacts when a caller supplies a primary‑key value that does not match any existing row, and whether retrying the same operation can create duplicate records.
save performs an INSERT if the primary‑key is null or not found, otherwise an UPDATE.save perform an INSERT or throw an exception?save decide whether to re‑execute an INSERT or an UPDATE, and can this lead to duplicate rows?When a primary‑key value is supplied but no row with that key exists, Zend_Db_Table::save attempts an UPDATE. The UPDATE affects zero rows, so the method returns false (or 0) and does not throw an exception. It does not automatically fall back to an INSERT.
update() is called.save falls back to insert(), which creates a new row (and a new auto‑increment value if applicable).save call with the same missing key repeats the UPDATE, again affecting zero rows – no duplicate row is created.id INT PRIMARY KEY).save on a row object where id is set to a value that does not exist in the table.null (or omit it) and call save twice; each call will perform an INSERT and generate two distinct rows.To confirm whether duplicates could appear in your specific setup, please tell us whether the primary key column is defined as AUTO_INCREMENT (or a sequence) and is supplied by the application, or whether it is a user‑assigned key that you expect to exist. This determines if a retry with a null key could cause duplicate inserts.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.