We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25586
    • 105 Posts
    I’m working on a snippet, but I cant get the addOne function to work.

    Here is my schema:
    	<object class="Companies" table="supdesk_companies" extends="xPDOSimpleObject">
    		<field key="name" dbtype="varchar" phptype="string" precision="100" null="false" default="" />
    
        	       <composite alias="Locations" class="Locations" local="id" foreign="company_id" cardinality="many" owner="local" />
        </object>
    
    	<object class="Locations" table="supdesk_locations" extends="xPDOSimpleObject">
                    <field key="company_id" dbtype="int" precision="10" phptype="integer" null="false" default="" />
    		<field key="city" dbtype="varchar" phptype="string" precision="100" null="false" default="" />
            
            <aggregate alias="Company" class="Companies" local="company_id" foreign="id" cardinality="one" owner="foreign" />
        </object>
    
    


    getOne works like a charm, but addOne doesn’t.

    If a use:
    $newcompany = $modx->newObject('Companies', array(
    	'name' => 'My Company Name'
    ));
    
    $newlocation = $modx->newObject('Locations', array(
    	'city' => 'Amsterdam'
    ));
    
    $newcompany->addOne($newlocation);
    $newcompany->save();
    

    - The Companytable is added in phpmyadmin and the company is added with the name ’My Company Name’.
    - The new locationtable is added in phpmyadmin, but the location is not added.

    Does anyone knows how this is possible?

    ----
    By the way: I can use $newlocation->save(); and the location gets saved, but I need to link the two.
    ----

    I’m working with MAMP, and I’m using modx revolutino 2.08

    Regards,

    Rogier
      • 22303 MODX Staff
      • 10,725 Posts
      You need to use addMany() for relations with cardinality of "many" — addOne for cardinality of "one".
        • 25586
        • 105 Posts
        Hahaha, never thought It would be that easy!

        Thanks, it works like a charm!