错误:在数组Symfony 3上调用成员函数contains()

时间:2017-08-05 09:11:29

标签: php symfony doctrine-orm symfony-3.3

我试图检查实体列表是否包含实体,但它给了我以下错误:

Error: Call to a member function contains() on array

这是我的功能代码:

 public function findAllEquivalents($reference)
{
    $equivalents =  $this->getEntityManager()
        ->createQuery(
            'SELECT p FROM EKPlateformeBundle:Piece p where p.reference = 
            :ref')
        ->setParameter('ref',$reference)
        ->getResult();

    $listFinal = $equivalents ; 
    foreach ($equivalents as $piece)
    {
        $autreEquivalents = $piece->getEquivalents();
        foreach ($autreEquivalents as $p)
        {
            if (!$listFinal->contains($p)) //  error here 
            {
               $listFinal->add($p);
            } 
        }
    }       
    return $listFinal ; 
}

我正在使用Symfony 3.3.2和doctrine2。 感谢。

1 个答案:

答案 0 :(得分:0)

编辑:更正代码

  public function findAllEquivalents($reference)
{
    $equivalents =  $this->getEntityManager()
        ->createQuery(
            'SELECT p FROM EKPlateformeBundle:Piece p where p.reference = :ref')
        ->setParameter('ref',$reference)
        ->getResult();

    $listFinal = $equivalents ; 
    //array_push($listFinal, $listFinal);

    foreach ($equivalents as $piece)
    {
        $autreEquivalents = $piece->getEquivalents();
        foreach ($autreEquivalents as $p)
        {
            if (!in_array($p, $listFinal))
            //if (!$listFinal->contains($p))
            {
                array_push($listFinal, $p);          
            } 
        }
    }       
    return $listFinal ; 
}