Cakephp 如何通过筛选子模型数据来删除父模型数据? 任务

Cakephp 如何通过筛选子模型数据来删除父模型数据? 任务,cakephp,cakephp-2.2,Cakephp,Cakephp 2.2,我试图根据相关模型中的条件返回一组数据 问题 目前,我能得到的最接近的方法是使用Containable返回所有匹配的模型数据,但仅返回与contain条件匹配的子数据。这并不理想,因为我的数据仍然包含主模型数据,而不是将其删除 我正在使用HABTM关系,例如,产品和类别之间,我希望查找特定类别中的所有产品 初始思想 基本方法是使用containable $this->Product->find('all', array( 'contain' => array(

我试图根据相关模型中的条件返回一组数据

问题 目前,我能得到的最接近的方法是使用Containable返回所有匹配的模型数据,但仅返回与contain条件匹配的子数据。这并不理想,因为我的数据仍然包含主模型数据,而不是将其删除

我正在使用HABTM关系,例如,
产品
类别
之间,我希望查找特定类别中的所有产品

初始思想 基本方法是使用containable

$this->Product->find('all', array(
    'contain' => array(
        'Category' => array(
            'conditions' => array(
                '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
此查询是正确的,只是连接条件被引用为“而不是”,这会中断查询

人工查询
问题在于我定义联接条件的方式。它不是一个关联数组,而是一个字符串

        'conditions' => array(
            'CategoriesProduct.product_id' => 'Product.id'
        )
更改为

        'conditions' => array(
            'CategoriesProduct.product_id = Product.id'
        )
        'conditions' => array(
            'CategoriesProduct.product_id' => 'Product.id'
        )
        'conditions' => array(
            'CategoriesProduct.product_id = Product.id'
        )