开发者

How Doctrine saves new related objects

开发者 https://www.devze.com 2022-12-27 04:42 出处:网络
If I create a new Doctrine object, with lots of relations, should I save() these relations before assigning them to newly created object?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消