spi*_*iil 13 arraycollection symfony doctrine-orm
如何检查Doctrine Collection(ManyToMany关系)字段中是否存在给定值?
例如,我尝试:
$someClass = $this->
getDoctrine()->
getRepository('MyBundle:MyClass')->
find($id);
if (!$entity->getMyCollectionValues()->get($someClass->getId())) {
$entity->addMyCollectionValue($someClass);
}
Run Code Online (Sandbox Code Playgroud)
但这当然不正确.那么,如何避免重复键?
Air*_*ram 30
你可以这样做:
$object = $this->getDoctrine()->getRepository('MyBundle:MyClass')->find($id);
if ( !$entity->getMyCollectionValues()->contains($object) ) {
$entity->addMyCollectionValue($object);
}
Run Code Online (Sandbox Code Playgroud)
您可以在http://www.doctrine-project.org/api/common/2.1/class-Doctrine.Common.Collections.ArrayCollection.html中查看 Doctrine ArrayCollection的可用功能.