Error: Call to a member function contains() on array Symfony 3
Error: Call to a member function contains() on array Symfony 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。
谢谢
已编辑:更正代码
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 ;
}
我正在尝试检查实体列表是否包含实体,但出现以下错误:
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。 谢谢
已编辑:更正代码
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 ;
}