fix(xpdo): Fix Illegal offset type for compound PK with generated field (#129)#272
Conversation
…ld (modxcms#129) - xPDOObject::save(): find generated field in compound PK and set value correctly - xPDOManager (MySQL): add AUTO_INCREMENT for generated fields in compound PK - Add NumberSeq test model and testSaveCompoundPkWithGeneratedField test - Fix variable initialization in testGetObjectGraphsByPK tests - Fix typo 'retreiving' -> 'retrieving' in test messages
- Add fallback in xPDOObject::save() to set _new=false when inserted with generated PK - Add NumberSeq to testGetDescendants expected array and SQLite schema - Create sqlite/NumberSeq.php for cross-platform test consistency
3f50fb4 to
0094028
Compare
…) failures - xPDOObject::save(): add scalar PK branch alongside compound PK iteration so the generated-key assignment is correct for both PK types - xPDOObject::save(): replace silent elseif fallback (which could mark an object persisted with a zero PK) with a LOG_LEVEL_WARN log call - mysql/xPDOManager::getColumnDef(): move phptype check to emission site; use getIndexMeta()['PRIMARY']['columns'] directly instead of iterating all indexes - Remove pgsql/sqlite NumberSeq model files: compound PK with a native- generated field is MySQL-only; those schemas defined a different table and implied cross-driver coverage that does not exist - Fix testSaveCompoundPkWithGeneratedField(): use $obj->get() accessors rather than named keys on getPrimaryKey(), which returns a numerically indexed array for compound PKs
opengeek
left a comment
There was a problem hiding this comment.
Thanks for this, @Ibochkarev! The fix is good. Tracing the "Illegal offset type" warning back to lastInsertId() returning an empty string on compound-PK objects with a generated field, and correcting the key assignment in save(), is the right approach.
I've pushed a commit on top before merging. I'm experimenting with a "fix-merge" approach on this project. By pushing small corrections directly onto contributor branches rather than asking for another round-trip, I'm hoping we can reduce friction and increase velocity on the project.
There are essentially two changes in the addendum:
1. Silent fallback replaced with a log warning.
After the generated key is assigned, your elseif block cleared _new even when lastInsertId() returned 0 — a zero-PK insert would silently look like a successful save. The addendum replaces that block with a LOG_LEVEL_WARN log call so callers get visibility into the failure. The success path (_new = false when a real ID comes back) is untouched.
2. Removed the pgsql and sqlite NumberSeq model files.
The pgsql and sqlite variants omit generated="native" on the key field, so they don't exercise this fix on those drivers. Keeping them would imply cross-driver coverage that doesn't exist. The addendum removes those six files and scopes this fix to MySQL. PostgreSQL (sequences) and SQLite (ROWID aliasing) both need manager-level changes — those belong in a follow-on PR.
Since this puts a commit on your branch that you didn't write, I'm flagging it before the merge proceeds. If anything in the addendum looks wrong or raises a concern, let me know.
Hello! I don't have anything against this, as it speeds up the decision-making process regarding the PR and its integration into the release branch. |
Fix PHP warning "Illegal offset type" when saving objects with compound primary key that includes a generated field (e.g. AUTO_INCREMENT).
Problem
When
getPK()returns an array for compound keys, the code used it as array index:$this->_fields[$this->getPK()]— PHP does not allow arrays as array keys.Solution
generated="native", and set only that field withgetGeneratedKey()getColumnDefto add AUTO_INCREMENT for generated fields in compound PK (only for the first column in PK, per MySQL requirement)Additional changes
testSaveCompoundPkWithGeneratedFieldtesttestGetObjectGraphsByPK/testGetObjectGraphsJSONByPKto avoid undefined variable noticesFixes #129