xpdo icon indicating copy to clipboard operation
xpdo copied to clipboard

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

Open kristk opened this issue 8 years ago • 2 comments

I get this PHP warning when i try to save an object that uses a compound primary key: (ERROR @ .../core/xpdo/om/xpdoobject.class.php : 1478) PHP warning: Illegal offset type

The warning is produced in modx 2.6.0 running om PHP 7.1.13.

I have a minimal table that uses this schema:

<?xml version="1.0" encoding="UTF-8"?>
<model package="trippeln" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
  <object class="NumberSeq" table="trippeln_number_seq" extends="xPDOObject">
    <field key="level" dbtype="enum" precision="'G','S','B'" phptype="string" null="false" index="pk" />
    <field key="number" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="pk"  generated="native" />

    <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
      <column key="level" length="" collation="A" null="false" />
      <column key="number" length="" collation="A" null="false" />
    </index>
  </object>
</model>

Here is an image showing the table structure in phpMyAdmin: table-structure

The modx snippet code that produces the warning is:

$number = $modx->newObject('NumberSeq');
$number->fromArray(array('level' => 'B', 'number' => NULL,), '', true);
$number->save();

The code line that is referenced (https://github.com/modxcms/xpdo/blob/2.5.x/xpdo/om/xpdoobject.class.php#L1478) uses the result from getPK() as an array index but since getPK() in this case returns an array that produces the warning. The save method should be changed so that it can handle compund keys as well but I don't know the best way to do it.

My code works in that way that the object is saved to the database correctly.

kristk avatar Jan 15 '18 21:01 kristk

Thanks for the issue. xPDO can handle compound keys, but the problem here is I never anticipated a case where a compound key would include an auto_increment value. I'll have to refactor the code there to handle this kind of situation.

opengeek avatar Jan 15 '18 21:01 opengeek

Was this ever resolved or is a workaround available? At the moment I have to try...catch the save() method to prevent the script from exiting.

verstratenbram avatar Jan 19 '23 13:01 verstratenbram