如何检查“教义集合”(多对多关系)字段中存在给定值?

例如,我尝试:

$someClass = $this->
             getDoctrine()->
             getRepository('MyBundle:MyClass')->
             find($id);

if (!$entity->getMyCollectionValues()->get($someClass->getId())) {

    $entity->addMyCollectionValue($someClass);

}


但这当然是不正确的。那么,如何避免重复的键?

最佳答案

您可以这样做:

$object = $this->getDoctrine()->getRepository('MyBundle:MyClass')->find($id);

if ( !$entity->getMyCollectionValues()->contains($object) ) {
    $entity->addMyCollectionValue($object);
}


您可以在http://www.doctrine-project.org/api/common/2.1/class-Doctrine.Common.Collections.ArrayCollection.html中查看Doctrine ArrayCollection的可用功能。

关于symfony - 教义2:检查教义集合中是否存在值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28252955/

10-12 16:28