Related objects
XPDO provides a number of generic methods for dealing with related objects in various types of relationships. First, let's look at adding related objects and the various relationships that can be defined in your object model.
Adding related objects
Adding objects that are related to another object in a classic foreign key relationship is very easy with XPDO, and can can be accomplished with the following two methods.
$person->addOne($anObject, 'local_fk_field_name');
$person->addMany($anObjectOrCollectionOfObjects, 'foreign_fk_field_name');
In both cases, the second argument, with the name of either the local or foreign field name for the FK relationship, is only required if the object has two foreign key definitions related to the same class. Here is a simple example with a one-to-many relationship between a Person and many Phones.
$person= $xpdo->getObject('Person', 1);
$phone= $xpdo->newObject('Phone');
$phone->set('type', 'home');
$phone->set('number', '+1 555 555 5555');
$phone->set('date_modified', date('Y-m-d H:i:s'));
$personPhone= $xpdo->newObject('PersonPhone');
$personPhone->set('person', $person->getPrimaryKey());
$personPhone->addOne('phone', $phone);
$personPhone->set('is_primary', false);
$person->addMany($personPhone);
$person->save();