Reputation: 25
I'm trying to add a row, that has a related (one to one) row attached to it, without having to do a lookup. For instance:
$row = new Product();
$row->setField1('123');
$row->setRelatedFieldId(3);
What I have to do now is:
$related_record = $repo->find(3);
$row->setRelatedName($related_record);
But symfony does a second search there. This is great functionality for the find, being able to link across the different tables, but I already have the related row's id, I'd rather not have to do another query on it before doing an insert. Any suggestions?
Upvotes: 1
Views: 54
Reputation: 48865
Replace:
$related_record = $repo->find(3);
With:
$related_record = $em->getReference('ClassName',123);
That gives you a reference to the related object which you can then use to set relations. The object itself will not be loaded. It is up to to you to ensure that object 123 exists.
I should point out that what with buffering and caching, you probably won't save much by using a reference.
Upvotes: 2
Reputation: 673
Please read http://www.doctrine-project.org/jira/browse/DDC-357 Looks like this is expected behavior and easiest way to use one2many relation inseted one2one
Upvotes: 0