我正在尝试为下面的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来完成,但想通过自定义查询来实现。

最佳答案

您是否尝试过使用容器?

$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'
    ),
));

关于php - Cakephp自定义查询,其中左联接表行为嵌套数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13703513/

10-09 20:23