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

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看