douhui3330 2015-08-27 09:02
浏览 64
已采纳

CakePHP加入内部

I need to do the following query using the CakePHP find method:

SELECT * FROM `ads`
INNER JOIN fields_values ON fields_values.ref_id = ad.id 
WHERE ad.active = 1

I tried this function, but she doesn't work :

$ads = $this->Ad->find('all', array(
            'joins' => array(
                array(
                    'table' => 'fields_values',
                    'alias' => 'fv',
                    'type' => 'INNER',
                    'conditions' => array(
                        "Ad.id = fv.ref_id "
                    )
                )
            ),
            'conditions' => array(
                'Ad.active' => 1
            ),
        ));
  • 写回答

1条回答 默认 最新

  • dtp19819 2015-08-27 09:05
    关注

    your query is wrong it should be

    SELECT * FROM `ads`
    INNER JOIN fields_values as fv ON fv.ref_id = ads.id 
    WHERE ads.active = 1
    

    Now you can build it up with cake query. Which left join also does it.

    $ads = $this->Ad->find('all', array(
                'conditions' => array(
                    'Ad.active' => 1
                ),
                'joins' => array(
                    array(
                        'table' => 'fields_values',
                        'alias' => 'fv',
                        'type' => 'LEFT',
                        'conditions' => array(
                            "fv.ref_id = Ad.id",
                            // or you can add the top condition here
                            //'Ad.active' => 1
                        )
                    )
                ),
            ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数