[英]Doctrine Magic Finders not working correctly - Symfony2
我正在對 Doctrine 查詢進行一些研究,因為我需要根據兩個子句獲得一些結果,在本例中為“類型”和“客戶端”。 我在此頁面上閱讀了 Magic Finders 並使用該邏輯創建了以下調用:
$collections = $repo->findByTypeAndClient('collection',$client);
因為我的表中有名為 type 和 client 的字段。 但是,當我運行腳本時,出現以下錯誤:
實體“AppBundle\\Entity\\CollectionDelivery”沒有字段“typeAndClient”。 因此,您不能在實體的存儲庫上調用“findByTypeAndClient”
這讓我覺得我可能做錯了什么,或者我遺漏了一些明顯的東西?
我正在使用 Symfony2 - 任何幫助表示贊賞! 謝謝!
是的,你做錯了什么^^
魔術方法僅適用於實體的單個字段。 你可以做 :
$collections = $repo->findByType('collection');
$collections = $repo->findByClient($client);
但是,如果您想要按類型和客戶端進行自定義查找,則必須在 repo 中添加一個方法:
public function findByTypeAndClient($type, $client) {
$qb = $this->createQueryBuilder('c');
$qb->where('c.type = :type')
->where('c.client = :client')
->setParameter(':type', $type)
->setParameter(':client', $client);
return $qb->getQuery()->getResult();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.