所以我和我的朋友正在讨论是否在 DQL 中使用 object 或 id 我在互联网上搜索但我在学说文档中找不到任何东西来查看哪个是最佳实践所以问题是这样的想象你有 Category 对象你想要获得价格低于 10 的产品,因此为了获得更好的性能,您在产品存储库中创建 DQL 并传递 $category 对象我的朋友说我们应该传递 $category->getId() 并像这样按 Id 搜索
public function findCheapProducts($categoryId){
return $this->createQueryBuilder('p')
->where('p.category = :categoryId')
->andWhere('p.price < :price')
->setParameters(array(
'categoryId' => $categoryId,
'price' => 10
));
}
但我说的是这样的
public function findCheapProducts(Category $category){
return $this->createQueryBuilder('p')
->where('p.category = :categoryId')
->andWhere('p.price < :price')
->setParameters(array(
'categoryId' => $category,
'price' => 10
));
}
我在这里写了代码,所以错误或事情对我来说并不重要目标是看看哪个是通过标识符放置条件的更好方法