1

我正在尝试为下面的 Cakephp 自定义查询获取嵌套数组:

$this->query("
                SELECT *
                FROM group_buys GroupBuy
                LEFT JOIN products Product
                ON Product.id = GroupBuy.product_id
                LEFT JOIN group_buy_users GroupBuysUser
                ON GroupBuysUser.group_buy_id = GroupBuy.id
                LEFT JOIN group_buy_images GroupBuyImage
                ON GroupBuyImage.group_buy_id = GroupBuy.id
                LEFT JOIN product_details ProductDetail
                ON ProductDetail.product_id = Product.id
                LEFT JOIN specifications Specification
                ON Specification.id = ProductDetail.specification_id
                LEFT JOIN specification_categories SpecificationCategory
                ON SpecificationCategory.id = Specification.specification_category_id
                WHERE GroupBuy.id = {$id}
            ");

问题在于它显然会产生冗余数据,而 GroupBuy 表行值重复我不想要。

如果 LEFT JOINED 表的行数比使用 Cake 的自定义查询的前表多,我们是否可以使用嵌套数组?

我知道这可以通过 find recursive = 2 来完成,但希望通过自定义查询来实现。

4

1 回答 1

0

您是否尝试过使用可包含的?

$this->GroupBuy->Behaviors->attach('Containable');

$this->GroupBuy->find('all', array(
    'conditions' => array('GroupBuy.id' => $id),
    'contain' => array(
        'Product' => array(
            'ProductDetail' => array(
                'Specification' => array(
                    'SpecificationCategory'
                )
            )
        ),
        'GroupBuysUser',
        'GroupBuyImage'
    ),
));
于 2012-12-04T13:20:50.347 回答