I have 2 objects mapped ,
Insert
class Box{ //[...] /** * @ORM\ManyToMany(targetEntity="Candy", cascade={"remove"}) * @ORM\OrderBy({"power" = "DESC"}) * @ORM\JoinTable(name="box_candies", * joinColumns={@ORM\JoinColumn(name="box_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="candy_id", referencedColumnName="id", unique=true)} * ) */ private $candies; }
And candy
class Candy { private $id; private $name;
As you can see, this is One-way, one-way, with table join . Box can βstoreβ candy, but Candy knows nothing about Box (where it is).
Now I have a page where I can make candy , and there is a form and standard isValid() and after that:
$box->addCandy($candy); $entity_manager->persist($candy); $entity_manager->persist($box); $entity_manager->flush();
Now where is my problem?
I would like Box to be able to store unique candies only (by name ), which means that Box can store Candy objects with the names "Choco" and "Orange" " , but cannot " mayonnaise "and" mayonnaise "
When creating candies, I cannot check with the UniqueEntity constraint because the candies are not aware of the gender. I was thinking of a Callback validator for Box or creating my own Constraint, but I think it's better to ask:
How can I do it?
source share