我試圖讓ID列表(一拉find('list')
)名單,但無法弄清楚如何與相關模型的條件做。CakePHP中找到相關模型的條件
我的聯繫是: Product hasAndBelongsToMany Category
Product belongsTo Manufacturer
我想找到的是分類ID列表,並有一定的製造商seo_url產品ID列表。我已經嘗試過大量的包含條件,引用其他模型的條件,甚至嘗試使用find('all')來查看我的條件是否可行,但是什麼也得不到。
我設法得到一個與產品展示 - >查詢(),但它不是以列表的形式。我可以在數組中循環並獲取ID或其他內容,但感覺非常不合適。我想確保Cake仍然對我的查詢執行所有的安全檢查,以及其他任何automagic。下面是我得到了工作查詢:
$all_results = $return['Category']['all_results'] = $this->Product->query('
SELECT `Product`.`id`
FROM `categories_products` as `CategoriesProduct`
JOIN `products` as `Product` ON (`CategoriesProduct`.`product_id` = `Product`.`id`)
JOIN `manufacturers` as `Manufacturer` ON (`Product`.`manufacturer_id` = `Manufacturer`.`id` AND `Manufacturer`.`seo_url` = \''.$manufacturer.'\')
WHERE `CategoriesProduct`.`category_id` IN ('.implode(',', $search_categories).')
');
這將返回
[all_results] => Array
(
[0] => Array
(
[Product] => Array
(
[id] => 101787
)
)
[1] => Array
(
[Product] => Array
(
[id] => 100781
)
)
[2] => Array
(
[Product] => Array
(
[id] => 101887
)
)
[3] => Array
(
[Product] => Array
(
[id] => 101888
)
)
)
注:$search_categories
是列表類的ID(即:array(12,42,24,5)
)
確實非常不可靠。 – 2011-05-16 21:00:24