We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42524
    • 12 Posts
    Hello,

    I'm having some problems with my custom databases. I've never had this problem before and can't seem to figure it out.

    So the problem is that i can't get the primary key of objects when they have the fieldname id...

    In a normal situation i would retrieve the id of an object with

    $obj->getPrimaryKey();
    $obj->id;
    $obj->lastInsertId();
    


    None of these work, except when i change the name to something like page_id.

    I'm not sure if it has something to do with xPDOSimpleObject and xPDOObject..


    • The components are build with CMPgenerator.
    • Cache cleared from the manger and manualy.
    • MODX version is 2.2.15, updated form 2.2.14 (2.2.14 had the same problem, tried updating)
    • Can't start over, its a running project

    If some one has a solution i would love to here it.
      • 40706
      • 128 Posts
      Sounds like your Object is an xPDOObject without an primaryKey definition

      look at this example, its important to define the field as pk and also create an primary index
      <object class="Users" table="users" extends="xPDOObject">
              <field key="user_id" dbtype="int" precision="11" phptype="integer" null="false" index="pk"  generated="native" />
              <field key="username" dbtype="varchar" precision="255" phptype="string" null="true" />
      
              <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
                      <column key="user_id" collation="A" null="false" />
              </index>
      </object>

      source: http://rtfm.modx.com/xpdo/2.x/getting-started/creating-a-model-with-xpdo/defining-a-schema/more-examples-of-xpdo-xml-schema-files

        • 3749
        • 24,544 Posts
        This might also help: http://bobsguides.com/blog.html/2013/06/12/xpdoobject-versus-xpdosimpleobject/. You may want your object to extend xPDOSimpleObject.
          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
          • 42524
          • 12 Posts
          Thanks for the replies,

          At this moment i don't have enough time to test your answers, but i'm surely going the read the article.

          The problem i'm having is not with the xPDOObject but with the xPDOSimpleObject, below i've but my current (partial) schema which is working. When i change car_id to id it (and rebuild the schema) it fails to work. I started out with id, which wasn't working, when i changed it to car_id it began to work.

          <?xml version="1.0" encoding="UTF-8"?>
          <model package="cars" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
          	<object class="CarsDreamData" table="cars_dream_data" extends="xPDOObject">
          		<field key="car_id" dbtype="int" precision="11" phptype="integer" null="false" index="pk"  generated="native" />
          		<field key="fullname" dbtype="varchar" precision="255" phptype="string" null="false" />
          
          		<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
          			<column key="car_id" length="" collation="A" null="false" />
          		</index>
          	</object>
          </model>
          
          • I seem to remember that one of those automatically adds an auto-increment 'id' field, so you can't use that in your schema.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 3749
              • 24,544 Posts
              Are you sure you didn't at some point extend xPDOSimpleObject? That would automatically add an 'id' field.

              Using your own 'id' field when extending xPDOObject is fairly common and should work fine.

              Keep in mind that the class generator will not overwrite existing class and map files. You have to delete the class and map files whenever you update the schema (ClassExtender does this for you).

              You usually have to drop the table too.
                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