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

    each time I'm thinking "now I know a little bit about MODx" I come to the point where I find out that I don't know anything about MODx!
    Currently this happens while I try to extend my current schema with a second custom table with a "one to many relation".

    Here is the current schema which describes a single custom table:

    <?xml version="1.0" encoding="UTF-8"?>
    <model package="goodnews" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM">
        <object class="GoodNewsCategory" table="goodnews_categories" extends="xPDOSimpleObject">
            <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
            <field key="description" dbtype="text" phptype="string" null="false" default="" />
            <field key="public" dbtype="int" precision="1" attributes="unsigned" phptype="boolean" null="false" default="1" />
            <field key="usergroup" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
            <field key="uniquekey" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    
            <field key="createdon" dbtype="datetime" phptype="datetime" null="true" />
            <field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
            <field key="editedon" dbtype="datetime" phptype="datetime" null="true" />
            <field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
    
            <aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign" />
            <aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign" />
        </object>
    </model>
    


    I'd like to extend the schema with another custom table goodnews_groups. Each group could have many categories but a category can have only one group associated.
    Here is the new schema which I created using the one to many example in the xPDO 2.x tutorial - where I don't understand some things:

    <?xml version="1.0" encoding="UTF-8"?>
    <model package="goodnews" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM">
        <object class="GoodNewsGroup" table="goodnews_groups" extends="xPDOSimpleObject">
            <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
            <field key="description" dbtype="text" phptype="string" null="false" default="" />
            <field key="modxusergroup" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
    
            <field key="createdon" dbtype="datetime" phptype="datetime" null="true" />
            <field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
            <field key="editedon" dbtype="datetime" phptype="datetime" null="true" />
            <field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
    
            <aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign" />
            <aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign" />
    
            <composite alias="GoodNewsCategory" class="GoodNewsCategory" local="id" foreign="goodnewsgroup_id" cardinality="many" owner="local" />
        </object>
        
        <object class="GoodNewsCategory" table="goodnews_categories" extends="xPDOSimpleObject">
            <field key="goodnewsgroup_id" dbtype="int" precision="10" phptype="integer" null="false" />
            <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
            <field key="description" dbtype="text" phptype="string" null="false" default="" />
            <field key="public" dbtype="int" precision="1" attributes="unsigned" phptype="boolean" null="false" default="1" />
            <field key="usergroup" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
            <field key="uniquekey" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    
            <field key="createdon" dbtype="datetime" phptype="datetime" null="true" />
            <field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
            <field key="editedon" dbtype="datetime" phptype="datetime" null="true" />
            <field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
    
            <aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign" />
            <aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign" />
            
            <aggregate alias="GoodNewsGroup" class="GoodNewsGroup" local="???" foreign="???" cardinality="one" owner="foreign" />
        </object>
    </model>
    


    How should line 35 look like?

    Why do I have to set a relation from the categories table back to the groups table. Shouldn't the relation from the groups table to the categories table (line 16) be enough?

    What else is missing?

    Greetings,
    Martin

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

      Freelancer @bitego http://www.bitego.com
      ---
      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
      More infos here: http://www.bitego.com/extras/goodnews/
    • discuss.answer
      • 4172
      • 5,888 Posts
      isn't it

      <aggregate alias="GoodNewsGroup" class="GoodNewsGroup" local="goodnewsgroup_id" foreign="id" cardinality="one" owner="foreign" />


      then?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 10378
        • 375 Posts
        Quote from: Bruno17 at Feb 01, 2013, 05:43 AM
        isn't it

        <aggregate alias="GoodNewsGroup" class="GoodNewsGroup" local="goodnewsgroup_id" foreign="id" cardinality="one" owner="foreign">


        then?</aggregate>

        Thanks Bruno,
        this was the answer!

        I first didn't understand why I have to link forth and back between the two tables. Now I know it's only for xPDO purposes...
          Freelancer @bitego http://www.bitego.com
          ---
          GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
          More infos here: http://www.bitego.com/extras/goodnews/