+++ Creating related records
Accessing related records in Doctrine is easy: you can use exactly the same getters and setters as for the record properties.
You can use any of the three ways above, however the last one is the recommended one for array portability purposes.
$user->Email;
$user->get('Email');
$user['Email'];
When accessing a one-to-one related record that doesn't exist, Doctrine automatically creates the object. So for example the following code is possible:
$user = new User();
$user->name = 'some user';
$user->Email->address = 'some@one.info';
// saves the user and the associated email
$user->save();
When accessing one-to-many related records, Doctrine creates a Doctrine_Collection for the related component. Lets say we have users and phonenumbers and their relation is one-to-many. You can add phonenumbers easily as shown above:
$user = new User();
$user->name = 'some user';
$user->Phonenumber[]->phonenumber = '123 123';
$user->Phonenumber[]->phonenumber = '456 123';
$user->Phonenumber[]->phonenumber = '123 777';
// saves the user and the associated phonenumbers
$user->save();
Another way to easily create a link between two related components is by using Doctrine_Record::link(). It often happens that you have two existing records that you would like to relate (or link) to one another. In this case, if there is a relation defined between the involved record classes, you only need the identifiers of the related record(s):
// We keep track of the new phone number identifiers
$phoneIds = array();
// Some phone numbers are created...
$phone1 = new Phonenumber();
$phone1['phonenumber'] = '555 202 7890';
$phone1->save();
$phoneIds[] = $phone1['id'];
$phone2 = new Phonenumber();
$phone2['phonenumber'] = '555 100 7890';
$phone2->save();
$phoneIds[] = $phone2['id'];
// Some user is created...
$user = new User();
$user['name'] = 'Werner Mollentze';
$user->save();
// Let's link the phone numbers to the user, since the relation to Phonenumber exists for the User record...
$user->link('Phonenumber', $phoneIds);
If a relation to the User record class is defined for the Phonenumber record class, you may even do this:
// Some user is created...
$user = new User();
$user['name'] = 'wernerm';
$user->save();
// Some phone numbers are created and linked to the User on-the-fly...
// This is possible if a relation to User exists for the Phonenumber record
$phone1 = new Phonenumber();
$phone1['phonenumber'] = '555 202 7890';
$phone1->save();
// Let's link this Phonenumber to our User...
$phone1->link('User', array($user['id']));
// We create another phone number...
$phone2 = new Phonenumber();
$phone2['phonenumber'] = '555 100 7890';
$phone2->save();
// Let's link this Phonenumber to our User too...
$phone2->link('User', array($user['id']));
+++ Retrieving related records
You can retrieve related records by the very same {{Doctrine_Record}} methods as in the previous subchapter. Please note that whenever you access a related component that isn't already loaded Doctrine uses one SQL SELECT statement for the fetching, hence the following example executes 4 SQL SELECTs.
$user = $conn->getTable('User')->find(5);
print $user->Email['address'];
print $user->Phonenumber[0]->phonenumber;
print $user->Group[0]->name;
Much more efficient way of doing this is using DQL. The following example uses only one SQL query for the retrieval of related components.
$user = Doctrine_Query::create()
->from('User u')
->leftJoin('u.Email e')
->leftJoin('u.Phonenumber p')
->leftJoin('u.Group g')
->execute();
print $user->Email['address'];
print $user->Phonenumber[0]->phonenumber;
print $user->Group[0]->name;
+++ Updating related records
You can update the related records by calling save for each related object / collection individually or by calling save on the object that owns the other objects. You can also call {{Doctrine_Connection::flush}} which saves all pending objects.
$user->Email['address'] = 'koskenkorva@drinkmore.info';
$user->Phonenumber[0]->phonenumber = '123123';
$user->save();
// saves the email and phonenumber
+++ Deleting related records
You can delete related records individually be calling {{delete()}} on a record or on a collection.
$user->Email->delete();
$user->Phonenumber[3]->delete();
// deleting user and all related objects:
$user->delete();
Usually in a typical web application the primary keys of the related objects that are to be deleted come from a form. In this case the most efficient way of deleting the related records is using DQL DELETE statement. Lets say we have once again users and phonenumbers with their relation being one-to-many. Deleting the given phonenumbers for given user id can be achieved as follows:
$deleted = Doctrine_Query::create()
->delete()
->from('Phonenumber')
->addWhere('user_id = ?', array($userId))
->whereIn('id', $phonenumberIds);
->execute();
// print out the number of deleted phonenumbers
print $deleted;
Sometimes you may not want to delete the phonenumber records but to simply unlink the relations by setting the foreing key fields to null. This can ofcourse be achieved with DQL but perhaps to most elegant way of doing this is by using Doctrine_Record::unlink(). Please note that the unlink method is very smart. It not only sets the foreign fields for related phonenumbers to null but it also removes all given phonenumber references from the User object.
Lets say we have a User who has 3 Phonenumbers (with identifiers 1, 2 and 3). Now unlinking the Phonenumbers 1 and 3 can be achieved as easily as:
$user->unlink('Phonenumber', array(1, 3));
$user->Phonenumber->count(); // 1
+++ Working with related records
++++ Testing the existance of a relation
$obj = new Model();
if(isset($obj->Relation())) { // returns false
...
}
$obj->Relation = new Relation();
if(isset($obj->Relation())) { // returns true
...
}