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

    this is my schema:

    <?xml version="1.0" encoding="UTF-8"?>
    <model package="storefinder" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" phpdoc-package="storefinder" phpdoc-subpackage="model" version="1.1">
      <object class="sfStore" table="sfinder_stores" extends="xPDOSimpleObject">
        <field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
        <field key="address" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
        <field key="city" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
        <field key="state" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
        <field key="zip" dbtype="varchar" precision="10" phptype="string" null="false" default="0" index="index" />
        <field key="country" dbtype="varchar" precision="20" phptype="string" null="false" default="" />
        <field key="phone" dbtype="varchar" precision="20" phptype="string" null="false" default="" />
        <field key="fax" dbtype="varchar" precision="20" phptype="string" null="false" default="" />
        <field key="active" dbtype="int" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
        <alias key="postalcode" field="zip" />
        <index alias="name" name="name" primary="false" unique="false" type="BTREE">
            <column key="name" length="" collation="A" null="false" />
        </index>
        <index alias="zip" name="zip" primary="false" unique="false" type="BTREE">
            <column key="zip" length="" collation="A" null="false" />
        </index>
    <composite alias="StoreOwners" class="sfStoreOwner" local="id" foreign="store" cardinality="many" owner="local" />
      </object>
      <object class="sfOwner" table="sfinder_owners" extends="xPDOSimpleObject">
        <field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
        <field key="email" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
        <index alias="name" name="name" primary="false" unique="false" type="BTREE">
            <column key="name" length="" collation="A" null="false" />
        </index>
    <composite alias="StoreOwners" class="sfStoreOwner" local="id" foreign="owner" cardinality="many" owner="local" />
      </object>
      <object class="sfStoreOwner" table="sfinder_store_owners" extends="xPDOSimpleObject">
        <field key="store" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" index="index" />
        <field key="owner" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" index="index" />
        <index alias="store" name="store" primary="false" unique="false" type="BTREE">
            <column key="store" length="" collation="A" null="false" />
        </index>
        <index alias="owner" name="owner" primary="false" unique="false" type="BTREE">
            <column key="owner" length="" collation="A" null="false" />
        </index>
    <aggregate alias="Store" class="sfStore" local="store" foreign="id" cardinality="one" owner="foreign" />
    <aggregate alias="Owner" class="sfOwner" local="owner" foreign="id" cardinality="one" owner="foreign" />
      </object>
    </model>


    This is my snippet to join all owners to a certain store:

    $modx->addPackage('storefinder', MODX_CORE_PATH . 'components/storefinder/model/');
    
    $store = $modx->getObject('sfStore', 1);
    $owners = $modx->getCollection('sfOwner');
    
    $store->addMany($owners);
    $store->save();


    The snippet call is not successful and returns the following error:

    [2015-09-10 13:51:14] (ERROR @ /index.php) No foreign key definition for parentClass: sfStore using relation alias: sfOwner

    Any idea why this is happening? All of my other test cases like removing related objects etc are working fine... [ed. note: mojorisin last edited this post 8 years, 6 months ago.]
      • 47401
      • 295 Posts
      both the pk and fk have to be the same data type. Myisam doesnt support linking fk with pk. use innodb instead.
        • 38539
        • 18 Posts
        Thanks for the reply but this doesn't solve my problem. I am also wondering since all of my MODx tables are in the MyISAM type but the addMany or addOne is working for all resources, chunks, categories etc. relationships.

        For example, the following code is working and the tables that are affected are also in the MyISAM type. So changing to innoDB won't solve my problem:

        $category = $modx->getObject('modCategory',6);
        $chunks = $modx->getCollection('modChunk');
        
        $category->addMany($chunks);
        $category->save();


        Any other ideas? [ed. note: mojorisin last edited this post 8 years, 6 months ago.]
          • 47401
          • 295 Posts
          could you print_r both $store and $owners please and output it here, dont forget to use <pre></pre> tags wink
            • 47401
            • 295 Posts
            i think bob is probibly the best person to talk to in this instance
              • 38539
              • 18 Posts
              $store

              <pre>Array ( [id] => 1 [name] => Example Store [address] => Example Street [city] => Example City [state] => Example State [zip] => 12345 [country] => USA [phone] => 01234567 [fax] => 01234567 [active] => 1 [postalcode] => 12345 )</pre>

              $owners

              <pre>Array ( [id] => 2 [name] => Owner #1 [email] => [email protected] ) Array ( [id] => 3 [name] => Owner #2 [email] => [email protected] )</pre>
                • 3749
                • 24,544 Posts
                Did you maybe make a change to your schema without regenerating the class and map files? The schema itself is not used by MODX.
                  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
                  • 38539
                  • 18 Posts
                  No, I didn't modify anything after I generated the files. I tried it with CreateXpdoClasses Snippet as well as with the MIGXdb generator...no success. All the examples from https://rtfm.modx.com/xpdo/2.x/getting-started/creating-a-model-with-xpdo/defining-a-schema/more-examples-of-xpdo-xml-schema-files are leading to the same error when trying to create new related objects. However the removal of related objects is working fine. I have absolutely no idea what I am doing wrong. I am running MODx 2.4.0.
                    • 38539
                    • 18 Posts
                    Here's another example that is not working for me. I re-named the classes, aliases etc. to make the error more obvious.

                    Schema:

                    <?xml version="1.0" encoding="UTF-8"?>
                    <model package="ofc" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
                    <object class="classBlogposts" table="test_blogposts" extends="xPDOSimpleObject">
                    
                            <field key="content" dbtype="text" phptype="string" null="true" />
                    
                            <composite alias="aliasBlogpostsTags" class="classBlogpostsTags" local="id" foreign="blogpost" cardinality="many" owner="local" />
                    </object>
                    <object class="classTags" table="test_tags" extends="xPDOSimpleObject">
                    
                            <field key="name" dbtype="varchar" precision="255" phptype="string" null="true" />
                    
                            <composite alias="aliasTagsBlogposts" class="classBlogpostsTags" local="id" foreign="tag" cardinality="many" owner="local" />
                    </object>
                    <object class="classBlogpostsTags" table="test_blogposts_tags" extends="xPDOSimpleObject">
                            <field key="blogpost" dbtype="int" precision="11" phptype="integer" null="true" />
                            <field key="tag" dbtype="int" precision="11" phptype="integer" null="true" />
                     
                            <aggregate alias="Tags" class="classTags" local="tag" foreign="id" cardinality="one" owner="foreign" />
                            <aggregate alias="Blogposts" class="classBlogposts" local="blogpost" foreign="id" cardinality="one" owner="foreign" />
                    </object>
                    </model>


                    Snippet:

                    $modx->addPackage('ofc', MODX_CORE_PATH . 'components/ofc/model/');
                    
                    $blogpost = $modx->getObject('classBlogposts', 1);
                    $tags = $modx->getCollection('classTags');
                    
                    $blogpost->addMany($tags);
                    $blogpost->save();


                    Error log:

                    [2015-09-14 15:54:13] (ERROR @ /index.php) No foreign key definition for parentClass: classBlogposts using relation alias: classTags
                    [2015-09-14 15:54:13] (ERROR @ /index.php) No foreign key definition for parentClass: classBlogposts using relation alias: classTags

                    Maybe it is important to mention that the error msg is always printed twice as shown above...no idea if this is somehow related to my problem.
                      • 3749
                      • 24,544 Posts
                      I can't see anything wrong with what you're doing, although it's confusing to read because the convention is for object class names to be singular and the aliases to be plural and begin with a capital letter. Here's a possible solution (using your terms from the schema in the previous post):

                      $blogpost = $modx->getObject('classBlogposts', 1);
                      $blogpostId = $blogPost->get('id');
                      $tags = $modx->getCollection('classTags');
                       
                      foreach ($tags as $tag) {
                          $tagId = $tag->get('id');
                          $bpt = $modx->newObject('classBlogpostsTags');
                          $bpt->set('blogpost', $blogpostId);
                          $bpt->set('tag', $tagId);
                          $bpt->save();
                      }
                      


                      This should actually be a little faster than using addMany() and does essentially what it would do if it were working.

                      BTW, it's possible that your method would work with the addition of an alias:

                      $blogpost->addMany($tags, 'aliasBlogpostsTags');



                        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