We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7923
    • 4,213 Posts
    I cannot get the id of the object after save. I have tried get('id') and getPrimaryKey(), both return nothing. Only way to get it is from $modx->lastInsertId(). Why?

    Here's the schema for the object:
    <object class="ltMarker" table="marker" extends="ltObject">
        <field key="id" dbtype="bigint" precision="20" phptype="integer" attributes="unsigned" null="false" generated="native" />
        <field key="type" dbtype="int" precision="11" phptype="integer" null="false" />
        <field key="lat" dbtype="decimal" precision="17,14" phptype="float" null="false" />
        <field key="lng" dbtype="decimal" precision="17,14" phptype="float" null="false" />
        <field key="created_on" dbtype="datetime" phptype="datetime" null="true" />
        <field key="created_user_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="true" />
        <field key="created_user_ip" dbtype="varchar" precision="45" phptype="string" null="true" />
        <field key="updated_on" dbtype="datetime" phptype="datetime" null="true" />
        <field key="updated_user_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="true" />
        <field key="updated_user_ip" dbtype="varchar" precision="45" phptype="string" null="true" />
    
        <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
            <column key="id" length="" collation="A" null="false" />
        </index>
    </object>
    


    The class ltObject extends xPDOObject. Here's the code for saving:

    $schoolMarker = null;
    if (!empty($_POST['schoolMarker'])) {
      $schoolMarker = $modx->newObject('ltMarker');
      $schoolMarker->set('type', $schoolMarker::TYPE_SCHOOL_MARKER);
      $schoolMarker->set('lat', $_POST['schoolMarker']['lat']);
      $schoolMarker->set('lng', $_POST['schoolMarker']['lng']);
      $schoolMarker->set('created_on', strftime("%Y-%m-%d %H:%M:%S"));
      $schoolMarker->set('created_user_id', $modx->user->get('id'));
      $schoolMarker->set('created_user_ip', $schoolMarker->getIp());
      $schoolMarker->set('updated_on', strftime("%Y-%m-%d %H:%M:%S"));
      $schoolMarker->set('updated_user_id', $modx->user->get('id'));
      $schoolMarker->set('updated_user_ip', $schoolMarker->getIp());
      $schoolMarker->save();
      $output .= '<pre>ID:'.$schoolMarker->get('id').'</pre>';
    }
    $id = $schoolMarker->get('id');
    $output .= '<pre>ID:'.$id.'</pre>';
    $output .= '<pre>ID:'.$schoolMarker->getPrimaryKey().'</pre>';
    $output .= '<pre>ID:'.$modx->lastInsertId().'</pre>';
    return $output;
    


    It saves the object to the database table ok, but $schoolMarker->get('id') and $schoolMarker->getPrimaryKey() return nothing, $modx->lastInsertId() returns the key for the newly created object, but that seems "stupid" to retrieve it through $modx.

    Why the save() doesn't populate the id field for my object?

    This question has been answered by doze. See the first response.



      "He can have a lollipop any time he wants to. That's what it means to be a programmer."
    • discuss.answer
      • 7923
      • 4,213 Posts
      I also run into all kinds of problems when trying to update a record.

      Then I added index="pk" attribute to the schema file for the ID field and regenerated files. Now everything started to work. Functions getPrimaryKey and get('id') returns the ID correctly after save and updating records work fine.

      I'm now wondering why is this, because I read from the documentation earlier when creating the schema that index field was deprecated... but I guess it's still needed in the current revolution version? I'm using MODX Revolution 2.2.6-pl (advanced).

      From the docs:

      index (deprecated) — An optional field, when set, will add a type of index to the field. Some of the values are "pk", "index", and "fk".

      index is deprecated
      The index attribute is deprecated for Schema Version 1.1 and ignored when you generate your models as version 1.1. This is only valid for models with no version attribute (or the version set explicitly to 1.0).


      My schema is version 1.1 btw...

      And I tested it again by removing index="pk" attribute from the schema and regenerated classes.. everything breaks again.. then added index="pk" back and regenerated classes and all is well.

      So at least for me, in version 1.1 schema index="pk" seems to be mandatory for primary key fields for it to work.


        "He can have a lollipop any time he wants to. That's what it means to be a programmer."
        • 3749
        • 24,544 Posts
        Did you set an index on the pk in your table before running the script? I think that would have solved it, but I'm not sure.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting