php - 将左连接表行作为嵌套数组的 Cakephp 自定义查询

标签 php mysql cakephp

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

最佳答案

您尝试过使用 containable 吗?

$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/

相关文章:

javascript - 尝试通过 AJAX 发送号码,然后将它们添加到我的数据库中

php - Cakephp Mysql查询

mysql - CakePHP 3 : How to automatically get fields from leftJoin?

php - MySQL 复杂查询未返回正确结果

PHP 静态变量在页面加载后重新加载

php - 显示来自 Instagram PHP API 的用户信息

php - 来自 MySQL 和 PHP 脚本的 Paypal 定期付款

javascript - 在mysql中用多维数组存储$_POST

PHP/MySQLi - file_get_contents() 超出允许的最大数据包数

php - 如何替换 cakephp 密码哈希算法?