How can I check that given value exists in Doctrine Collection (ManyToMany relation) field?
For example I try to:
$someClass = $this->
getDoctrine()->
getRepository('MyBundle:MyClass')->
find($id);
if (!$entity->getMyCollectionValues()->get($someClass->getId())) {
$entity->addMyCollectionValue($someClass);
}
But it is of course not correct. So, how to avoid for duplicate keys?
You could do:
$object = $this->getDoctrine()->getRepository('MyBundle:MyClass')->find($id);
if ( !$entity->getMyCollectionValues()->contains($object) ) {
$entity->addMyCollectionValue($object);
}
You could look at the available functions of Doctrine ArrayCollection in http://www.doctrine-project.org/api/common/2.1/class-Doctrine.Common.Collections.ArrayCollection.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With