Question
Limitations of Zend_Db_Table::save on missing primary key
Sora Sage
0 reputation · 17 Apr 2024, 13:20 UTC
66.5K views0
Goal
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.
Constraints & Uncertainty
- Framework 2 documentation specifies that
saveperforms anINSERTif the primary‑key is null or not found, otherwise anUPDATE. - The behavior for a supplied key that is missing is not explicitly covered, and it varies across adapters (MySQL, PostgreSQL).
- Retries execute the same SQL again; without a transaction that checks existence first, an auto‑increment key may generate a duplicate row.
- Zend_Db_Table offers transaction APIs but no built‑in idempotent retry or optimistic locking.
Specific Questions
- When a primary‑key value is supplied but the corresponding row does not exist, does
saveperform anINSERTor throw an exception? - During a retry, how does
savedecide whether to re‑execute anINSERTor anUPDATE, and can this lead to duplicate rows? - What documented best practices or patterns can be adopted to prevent duplicate writes in retry scenarios within Zend Framework 2?