How Doctrine saves new related objects
If I create a new Doctrine object, with lots of relations, should I save() these relations before assigning them to newly created object? E.g.
$main = new Main();
$child = new Child();
$main->child_rel = $child; // do I need to save the child obj explicitly?
$main->save();
开发者_开发知识库
I assumed that parent will automatically call cascading saves, but this doesn't seem to be the case for a newly instantiated parent object.
How does it really work?
Doctrine take care of everything and save related records if needed. By the way, you don't need to instanciate an related object. You can use this syntax :
$user->Email->address = 'me@server.com';
$user->save();
In the case of one-to-many and many-to-many relationships :
$user->Phonenumbers[]->phonenumber = '123 123';
$user->Phonenumbers[]->phonenumber = '456 123';
$user->Phonenumbers[]->phonenumber = '123 777';
$user->save();
More infos on the doctrine documentation.
精彩评论