Skip to Content (c) Skip to Navigation (n) Skip to Search (s)

1.0-beta (rev 75) released!

xPDO is in beta. There are several API improvements, more code reductions and simplifications, and the introduction of SQLite and PostgreSQL driver support are coming very soon. xPDO is being used to develop the next-generation MODx CMF.

Forgotten credits...

I'd like to extend a special thanks to Andrea Giammarchi for his PDO for PHP 4 implementation that inspired me to begin coding this project. If not for PHPClasses.org and the inspiration I found in Andrea's work there, the OpenExpedio project would likely not exist, as I probably would have invested my time in working with PHPDoctrine.

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();