Commit 94c7b9f6 authored by zYne's avatar zYne

--no commit message

--no commit message
parent a58bec12
+++ Creating related records +++ Creating related records
When accessing related records and if those records do not exists Doctrine automatically creates new records. Accessing related records in Doctrine is easy: you can use exactly the same getters and setters as for the record properties.
<code type="php"> You can use any of the three ways above, however the last one is the recommended one for array portability purposes.
// NOTE: related record have always the first letter in uppercase
$email = $user->Email; <code type="php">
$user->Email;
$email->address = 'jackdaniels@drinkmore.info';
$user->get('Email');
$user->save();
$user['Email'];
// alternative: </code>
$user->Email->address = 'jackdaniels@drinkmore.info'; 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->save(); <code type="php">
</code> $user = new User();
$user->name = 'some user';
+++ Retrieving related records $user->Email->address = 'some@one.info';
// saves the user and the associated email
You can retrieve related records by the very same {{Doctrine_Record}} methods you've already propably used for accessing record properties. When accessing related record you just simply use the class names. $user->save();
</code>
<code type="php">
print $user->Email['address']; 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 relations is one-to-many. You can add phonenumbers easily as shown above:
print $user->Phonenumber[0]->phonenumber; <code type="php">
$user = new User();
print $user->Group[0]->name; $user->name = 'some user';
</code>
$user->Phonenumber[]->phonenumber = '123 123';
$user->Phonenumber[]->phonenumber = '456 123';
+++ Updating related records $user->Phonenumber[]->phonenumber = '123 777';
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. // saves the user and the associated phonenumbers
$user->save();
<code type="php"> </code>
$user->Email['address'] = 'koskenkorva@drinkmore.info';
+++ Retrieving related records
$user->Phonenumber[0]->phonenumber = '123123';
You can retrieve related records by the very same {{Doctrine_Record}} methods you've already propably used for accessing record properties. When accessing related record you just simply use the class names.
$user->save();
<code type="php">
// saves the email and phonenumber print $user->Email['address'];
</code>
print $user->Phonenumber[0]->phonenumber;
+++ Deleting related records print $user->Group[0]->name;
</code>
You can delete related records individually be calling {{delete()}} on each record. If you want to delete a whole record graph just call delete on the owner record.
<code type="php"> +++ Updating related records
$user->Email->delete();
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->Phonenumber[3]->delete();
<code type="php">
// deleting user and all related objects: $user->Email['address'] = 'koskenkorva@drinkmore.info';
$user->delete(); $user->Phonenumber[0]->phonenumber = '123123';
</code>
$user->save();
+++ Working with associations // saves the email and phonenumber
</code>
+++ Deleting related records
You can delete related records individually be calling {{delete()}} on each record. If you want to delete a whole record graph just call delete on the owner record.
<code type="php">
$user->Email->delete();
$user->Phonenumber[3]->delete();
// deleting user and all related objects:
$user->delete();
</code>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment