0
任务
我试图根据相关模型中的条件返回一组数据。如何通过筛选子模型数据来移除父模型数据?
问题
目前最接近我可以使用中可容纳返回所有匹配模型的数据,但只有孩子返回的数据,如果它匹配包含条件得到。这并不理想,因为我的数据仍然包含主要模型数据,而不是被删除。
我使用的是HABTM关系,例如Product
和Category
,我想查找特定类别中的所有产品。
初始创意
基本的方法是使用可容纳的。
$this->Product->find('all', array(
'contain' => array(
'Category' => array(
'conditions' => array(
'Category.id' => $categoryId
)
)
)
));
虽然这将返回所有产品,并且只是删除类别维度,如果它不符合包含条件。
最近到目前为止
$this->Product->find('all', array(
'contain' => false,
'joins' => array(
array(
'table' => 'categories_products',
'alias' => 'CategoriesProduct',
'type' => 'LEFT',
'conditions' => array(
'CategoriesProduct.product_id' => 'Product.id'
)
),
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'LEFT',
'conditions' => array(
'Category.id' => 'CategoriesProduct.category_id'
)
)
),
'conditions' => array(
'Product.status_id' => 1,
'Category.id' => $categoryId
),
));
产生下面的查询,
SELECT `Product`.`id`, `Product`.`name`, `Product`.`intro`, `Product`.`content`, `Product`.`price`, `Product`.`image`, `Product`.`image_dir`, `Product`.`icon`, `Product`.`icon_dir`, `Product`.`created`, `Product`.`modified`, `Product`.`status_id`
FROM `skyapps`.`products` AS `Product`
LEFT JOIN `skyapps`.`categories_products` AS `CategoriesProduct` ON (`CategoriesProduct`.`product_id` = 'Product.id')
LEFT JOIN `skyapps`.`categories` AS `Category` ON (`Category`.`id` = 'CategoriesProduct.category_id')
WHERE `Product`.`status_id` = 1
AND `Category`.`id` = 12
该查询是正确的,但连接条件被引述'而不是',它打破了查询。