Skip to content

fix(xpdo): Fix Illegal offset type for compound PK with generated field (#129)#272

Open
Ibochkarev wants to merge 3 commits intomodxcms:3.xfrom
Ibochkarev:fix/129-compound-pk-generated-field
Open

fix(xpdo): Fix Illegal offset type for compound PK with generated field (#129)#272
Ibochkarev wants to merge 3 commits intomodxcms:3.xfrom
Ibochkarev:fix/129-compound-pk-generated-field

Conversation

@Ibochkarev
Copy link
Contributor

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

  • xPDOObject::save(): Iterate over PK fields, find the one with generated="native", and set only that field with getGeneratedKey()
  • xPDOManager (MySQL): Extend getColumnDef to add AUTO_INCREMENT for generated fields in compound PK (only for the first column in PK, per MySQL requirement)

Additional changes

  • Add NumberSeq test model and testSaveCompoundPkWithGeneratedField test
  • Fix variable initialization in testGetObjectGraphsByPK / testGetObjectGraphsJSONByPK to avoid undefined variable notices
  • Fix typo "retreiving" → "retrieving" in test messages

Fixes #129

…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
@Ibochkarev Ibochkarev marked this pull request as ready for review March 11, 2026 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"PHP warning: Illegal offset type" when saving object with compound primary key

1 participant