Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to establish an association in Doctrine2 without target entity at hand?

There is a ManyToOne association defined between entities Pattern and Category (Pattern is an owning side of the relation). Category has many patterns, pattern belongs to one category. So there is a field Pattern.category with @ManyToOne Doctrine annotation.

Now, in my scenario I have the id of the Category entity (posted from form) that I want assign to Pattern.category field of the newly created Pattern (which will be persisted), but I don't want to load this Category entity - I don't need it, I just want to create a Pattern entity, assign it to a Category (which id I have), and persist it. It seems strange to me, that I have to load the Category entity just to establish the connection, when all I really need is just an id, which I already have.

Maybe it smells like using relational database concepts with ORM, but it seems completely pointless to load this entity just to establish connection, when I know id of that target entity.

I am new to Doctrine btw.

like image 620
Dawid Ohia Avatar asked Feb 22 '23 22:02

Dawid Ohia


1 Answers

You can use Reference Proxy:

$category = $em->getReference('Category', $id);
$pattern->setCategory($category);
like image 66
meze Avatar answered Apr 07 '23 03:04

meze