2015-01-09 44 views
2

我想从“Cours”表中获取所有记录,其中由表单提交的$speciality的值是IN arrayColloction()由每个返回类别Cours的单个对象。我使用的下一行有那个结果(但遗憾的是它不工作):可捕获的致命错误:类的对象...不能转换为字符串

public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality) 
    { 
    $qb 
     ->andWhere($qb->expr()->in(':speciality','a.specialities')) 
     ->setParameter('speciality', $speciality) ; 
    return $qb; 
    } 

CoursManyToMany关系就像下面的代码:

/** 
* @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 

回答

1

我解决问题,我在这里发布的解决方案,它可以帮助也许别人...... 你必须使用条款的“成员”对待一个多对多关系,你也许需要注入下面的代码directley在Expr.php文件:

/** 
* 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; 
} 

欲了解更多信息,请继续here(也许你需要的也用实例?与上面相同) 现在,在我的代码中,我使用了“isMemberOf”函数,并且工作正常。

public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality) 
    { 
    $qb 
     ->andWhere($qb->expr()->isMemberOf(':speciality','a.specialities')) 
     ->setParameter('speciality', $speciality) ; 
    return $qb; 
    } 

希望能有所帮助。

1

我没有测试这一点,但使用Member of应该解决的问题

public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality) 
    { 
    $qb 
     ->andWhere($qb->expr()->isMemberOf(':speciality','a.specialities')) 
     ->setParameter('speciality', $speciality) ; 
    return $qb; 
    } 
+0

现在我收到以下异常:[code] CRITICAL - 未捕获到PHP异常Symfony \ Component \ Debug \ Exception \ UndefinedMethodException:“尝试在C中的类”Doctrine \ ORM \ Query \ Expr“上调用方法”isMemberOf“ :\ wamp \ www \ Symfony \ src \ BacUp \ GeneralBundle \ Entity \ CoursRepository.php line 84.“在C:\ wamp \ www \ Symfony \ src \ BacUp \ GeneralBundle \ Entity \ CoursRepository.php第84行[/ code] – 2015-01-10 13:54:54

+0

现在,它能正常工作......谢谢。 – 2015-01-10 14:15:00

相关问题