2015-01-09 77 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

相關問題