Php 可捕获的致命错误:类的对象…无法转换为字符串
我试图从“Cours”表中获取所有记录,其中由表单提交的Php 可捕获的致命错误:类的对象…无法转换为字符串,php,symfony,orm,doctrine-orm,Php,Symfony,Orm,Doctrine Orm,我试图从“Cours”表中获取所有记录,其中由表单提交的$speciality的值位于arraycollection()中,该值由类Cours的每个单个对象返回。我正在使用下一行获得该结果(但不幸的是,它不起作用): 类Cours有一个manytomy关系,如下代码所示: /** * @ORM\ManyToMany(targetEntity="BacUp\GeneralBundle\Entity\Speciality", cascade={"persist"}) * @ORM\JoinCo
$speciality
的值位于arraycollection()
中,该值由类Cours
的每个单个对象返回。我正在使用下一行获得该结果(但不幸的是,它不起作用):
类Cours
有一个manytomy
关系,如下代码所示:
/**
* @ORM\ManyToMany(targetEntity="BacUp\GeneralBundle\Entity\Speciality", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
* @Assert\Count(min = 1, minMessage = "You have to choose at least one speciality")
*/
private $specialities;
执行返回以下错误:
CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Catchable Fatal Error: Object of class BacUp\GeneralBundle\Entity\Speciality could not be converted to string in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Expr.php line 452" at C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Expr.php line 452
我没有对此进行测试,但是使用
的成员应该可以解决问题
public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality )
{
$qb
->andWhere($qb->expr()->isMemberOf(':speciality','a.specialities'))
->setParameter('speciality', $speciality) ;
return $qb;
}
我解决了这个问题,我在这里发布了解决方案,它可能会帮助其他人。。。
您必须使用“MEMBER OF”子句来处理多个关系,并且可能需要在Expr.php文件中插入以下代码directley:
/**
* Creates an instance of MEMBER OF function, with the given arguments.
*
* @param string $x Value to be checked
* @param string $y Value to be checked against
*
* @return string
*/
public function isMemberOf($x, $y)
{
return $x . ' MEMBER OF ' . $y;
}
有关更多信息,请继续(可能您还需要使用的实例?与上面相同)
现在,在我的代码中,我使用了“isMemberOf”函数,它工作得很好
public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality )
{
$qb
->andWhere($qb->expr()->isMemberOf(':speciality','a.specialities'))
->setParameter('speciality', $speciality) ;
return $qb;
}
希望这能有所帮助。我现在遇到以下异常:[code]严重-未捕获的PHP异常Symfony\Component\Debug\exception\UndefinedMethodException:“试图调用方法”isMemberOf“在C:\wamp\www\Symfony\src\BacUp\GeneralBundle\Entity\CoursRepository.PHP第84行的类“条令\ORM\Query\Expr”中。”在C:\wamp\www\Symfony\src\BacUp\GeneralBundle\Entity\CoursRepository.php第84行[/code]
public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality )
{
$qb
->andWhere($qb->expr()->isMemberOf(':speciality','a.specialities'))
->setParameter('speciality', $speciality) ;
return $qb;
}