addOne
Last updated Dec 21st, 2019 | Page history | Improve this page | Report an issue
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$306 per month—let's make that $500!
Learn morexPDOObject::addOne()¶
Adds an object related to this instance by a foreign key relationship.
Syntax¶
API Docs: xPDOObject::addOne()
boolean addOne (
mixed &$obj,
[string $alias = '']
)
Example¶
Add a Rank to a newly-created Person, then save both through cascading.
$person = $xpdo->newObject('Person',1);
$person->set('fname','Johnny');
$person->set('lname','Benjamins');
$rank = $xpdo->newObject('Rank');
$rank->set('title','CEO');
$rank->set('level',1);
$person->addOne($rank);
$person->save(); // will save both person and rank
Troubleshooting¶
If you're having trouble using this function, it's helpful to increase the logging level:
$modx->setLogLevel(4); // show all debugging info
If you are getting errors like the following:
Foreign key definition for class , alias XXXXX not found, or cardinality is not 'one'.
then you should probably be using addMany() instead. Check your XML schema file for the object which is attempting to run addOne and verify that the relationship to the object you are trying to add is defined with cardinality="one".
See Also¶
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$306 per month—let's make that $500!
Learn more