我想知道一個主題。我們可以在 Laravel 中使用本地作用域,但我不知道是否適用于 Symfony。
檔案:Laravel 本地范圍
好吧,我的問題是我可以在 Symfony 中使用它嗎?這可能嗎 ?
祝你有美好的一天
uj5u.com熱心網友回復:
您可以對存盤庫中的經典方法執行相同操作。我可以向您展示一個示例(使用檔案中的源代碼):
public function findAllGreaterThanPrice(int $price, bool $includeUnavailableProducts = false): array
{
// automatically knows to select Products
// the "p" is an alias you'll use in the rest of the query
$qb = $this->createQueryBuilder('p')
->where('p.price > :price')
->setParameter('price', $price)
->orderBy('p.price', 'ASC');
if (!$includeUnavailableProducts) {
$qb->andWhere('p.available = TRUE');
}
$query = $qb->getQuery();
return $query->execute();
// to get just one result:
// $product = $query->setMaxResults(1)->getOneOrNullResult();
}
在這里,您可以回傳 $qb 而不是 return "$query->execute()",并且鏈式方法將可用。你可以這樣做:
$repo->findAllActive()->findAllGreaterThan12();
這里 $repo 將是注入控制器的存盤庫。
在這兩種方法中,您都只有一個 where 和一個 querybuilder 的回傳。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/371976.html
上一篇:Symfony:重定向行程的輸出
