Reputation: 81
I'm trying to create a relation ManyToMany with doctrine (in symfony) that is depending on a field value.
/**
* @ORM\ManyToMany(targetEntity="Label")
* @ORM\JoinTable(
* name="Item_Label",
* joinColumns={@ORM\JoinColumn(name="item_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="label_id", referencedColumnName="id")}
* )
*/
private $labels;
Here we understand that we have to get data from Label
via the table Item_label
We are on table Wine
Wine.id <-> Item_Label.item_id
<<< `WHERE Item_Label.item_type = 'wine'` >>>
`Item_Label.label_id` <-> `Label.id`
So, how can i write the WHERE Item_Label.item_type = 'wine'
in annotations ?
Or a SqlFilter (I tried but failed) ?
Thanks for your help =)
Upvotes: 1
Views: 172
Reputation: 10335
So, it seems from this SO answer that it's not possible in doctrine.
My workaround was to add a method to my entity class as follows
public function foo($itemLabelRepo)
{
$found = $itemLabelRepo->findBy(['item_type'=>'wine', 'label_id'=>$this->getId()]);
if(count($found)!=1) {
throw new \Exception("Found non-one object from entity role");
}
return $found[0];
}
where $itemLabelRepo would be the repository to "Item_Label" in your case
Upvotes: 0