如何检查项目是否与多对多关系中的某个其他项目相关?

时间:2015-07-04 15:36:22

标签: symfony

document entitydocumentstags entity有一对多关系。

我如何检查某些文件是否与某些"标签"的文件标签有关?列值?

文件实体:

/**
* @ORM\OneToMany(targetEntity="Documentstags", mappedBy="documentid")
*/
protected $tags;

Documentstags实体:

/**
     * @ORM\Column(type="string", length=220)
     */
    protected $tag;

我如何检查文档A是否与Documentstags项目相关,哪个标记值为例如乙

目前我已将以下代码实现为Documents entity的功能,但它似乎没有效果:

$tags = $this->getTags();
        $is_zatwierdzony = false;
        foreach($tags as $tag)
        {
            if($tag.tag == $this->avaliabletags['zatwierdzony']['name']) $is_zatwierdzony = true
        }

1 个答案:

答案 0 :(得分:2)

你可以使用Criteria类:

例如:

...
use Doctrine\Common\Collections\Criteria;
...

public function getMatchingTags() 
{
    $criteria = Criteria::create()
                ->where(Criteria::expr()->eq("tag", $this->avaliabletags['zatwierdzony']['name']));

    $tags = $this->getTags()->matching($criteria);

    #do something with matching tags
}

Doctrine2文档:Filtering Collections

相关问题