2
所以我和我的朋友正在討論是否在DQL中使用對象或ID 我在網上搜索過但我在教條文檔中找不到任何東西,看哪個是最佳實踐 所以,問題是這樣的 想象一下,你有分類對象 你想要得到的是有價格低於10,從而獲得更好的性能的產品,你做的產品信息庫中的DQL並通過$類別對象 我的朋友說,我們應通過$ category-> getId()並按此ID搜索在教條2中使用對象和標識符2 DQL
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
));
}
我這裏寫的代碼,這樣的錯誤或東西並不真正重要,我在這裏 的目的是看它是由標識符
把一個條件更好的辦法
我不會鍵入方法參數,命名爲'$ category',然後,ID或對象將工作 – AdrienBrault