duanmao1975 2016-02-01 04:25
浏览 7
已采纳

cakephp访问字段在具有关系模型的模型中搜索

Excuse me everybody that have a great skill in cakephp, i'am beginer in cakephp, i don't know how to access field which i want to use the value.

This is the array data show from controller

Array
(
`[Claim] => Array`
        (
            [id] => 121
            [name] => Gwoo the Kungwoo
            [created] => 2007-05-01 10:31:01
        )
    [ClaimDetail] => Array
        (
            [0] => Array
                (
                    [id] => 123
                    [claim_id] => 121
                    [title] => On Gwoo the Kungwoo
                    [body] => The Kungwooness is not so Gwooish
                    [date] => 2006-05-01 10:31:01
                )
            [1] => Array
                (
                    [id] => 124
                    [claim_id] => 121
                    [title] => More on Gwoo
                    [body] => But what of the 'Nut?'
                    [date] => 2006-05-01 10:41:01
                )
        )
);

in the controller i make the conditions,

$conditions = array(
                    'ClaimDetail.date between ? AND ?'  => array(
                        $this->request->query['start_date'],
                        $this->request->query['end_date']
                    ),
                    'Claim.delete_flag' => 0
                );

but cakephp show the error, unknown field,

this is the find,

$claim = $this->Claim->find('all', array(
                'conditions'    => $conditions
            ));



Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ClaimDetail.date' in 'where clause'


there're any other ways for me to make conditions like what i want,

thankss before and after... T_T stuck  one week 

  • 写回答

1条回答 默认 最新

  • douqiu0796 2016-02-01 04:59
    关注

    The problem is you're applying the condition on an associated model and not the original model. One way to solve this is to join the two tables.

       $joins   =   array(
                     array(
                        'table' => 'claim_details',
                        'alias' => 'ClaimDetail',
                        'type' => 'INNER',
                        'conditions' => array(
                             'Claim.id = ClaimDetail.claim_id',
                             'ClaimDetail.date between "'.$this->request->query['start_date'].'" AND "'.$this->request->query['end_date'].'" '
                         )
                     )
                );
    
       $claim = $this->Claim->find('all', array(
               'joins'       => $joins
            ));
    

    This should give you what you're looking for.

    Peace! xD

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历