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

    I followed Bob's Guide for creating a custom table and I am using it to store data generate with a FormIt form. That works perfectly. However, I have two problems and spent the last two hours searching for possible solution and couldn't find any. I know it is probably just a small issue and I would highly appreciate if anyone could help me out:

    1. I'm trying to list all of the rows from the table:

    $contacts = $modx->getCollection('Contacts', $c, false); 
    [...]
    foreach($contacts as $contact)...
    [...]
    $contact->get('name');
    


    That works perfectly. However, I have no idea how I can get the ID of the row. I tried:
    $contact->get('id');
    $contact->get('pk');
    $contact->get('primary_key');

    Any idea?

    2. Even though I can insert new rows without a problem, I can not update them. In the error.log I found the following error:
    No primary key value found for pk definition: 


    This is how I update:
    $contact = $modx->getObject('Contacts', array('id' => $_GET['contact_id']), false);
    $contact->set('name', $_POST['name']);
    $contact->save();
    


    $contact->save() returns true but the changes are not stored in the database. It thus seems that error 1 and 2 are somehow connected. My table schema does not represent any primary key but I read that xPDOSimpleObject automatically selects the id field for that?

    <?xml version="1.0" encoding="UTF-8"?>
    <model package="contact_form" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
    	<object class="Contacts" table="contacts" extends="xPDOSimpleObject">
    		<field key="name" dbtype="varchar" precision="255" phptype="string" null="true" />
    [...]
    	</object>
    </model>


    It thus seems to be an issue related to the primary key.

    Thanks in advance for your help.
      • 4172
      • 5,888 Posts
      does your table have this primary-key 'id' - field?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 43203
        • 5 Posts
        Yes.
          • 43203
          • 5 Posts
          Ok, solved it. The create_xpdo_classes.php creates a schema that does not seem to work properly. I manually added information for the primary key an it works. Similar thread:

          https://forums.modx.com/thread/82698/cannot-retrieve-id-from-the-object-after-save#dis-post-456403
            • 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
              • 43203
              • 5 Posts
              Hey Bob,

              yes, I did, but the field was just missing entirely in the schema. I thought that xPDOSimpleObject would deal with that automatically, but apparently it does not.
                • 4172
                • 5,888 Posts
                you don't need a id-field in your schema, when using xPDOSimpleObject.

                when you let create the table from schema (createObjectContainer) the field and primary should be created automatically.
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 43203
                  • 5 Posts
                  Well, they were not created. That is the whole point.
                    • 4172
                    • 5,888 Posts
                    Did a quick test with the MIGXdb - package - manager

                    used this schema
                    <?xml version="1.0" encoding="UTF-8"?>
                    <model package="contact_form" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
                        <object class="Contacts" table="contacts" extends="xPDOSimpleObject">
                            <field key="name" dbtype="varchar" precision="255" phptype="string" null="true" />
                            <field key="test" dbtype="varchar" precision="255" phptype="string" null="true" />
                    
                        </object>
                    </model>
                    


                    Did 'Parse schema' + 'Create Tables' and got this table:

                    CREATE TABLE IF NOT EXISTS `modx_contacts` (
                      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
                      `name` varchar(255) DEFAULT NULL,
                      `test` varchar(255) DEFAULT NULL,
                      PRIMARY KEY (`id`)
                    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
                    



                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 32699 ☆ A M B ☆
                      • 427 Posts
                      The correct syntax would be $objectName->getPrimaryKey();

                      As long as you are extending xPDOSimpleObject the id field will be added automatically.

                      You should also be able to $objectName->get('id');

                      Additionally, it is $modx->getCollection('Contacts');

                      Lastly,provide yourself a prefix for your schemata definitions so that you do not run into collisions with a Package having a Contacts object also.

                      Should be myContacts or something of your choosing.

                      Check out my site for some helping functions and cheatsheets on MODX and xPDO
                        Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

                        Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com