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

时间:2016-02-01 04:25:15

标签: php cakephp cakephp-2.0

对不起每个在cakephp都很有技巧的人,     我在cakephp的初学者,     我不知道如何访问我想使用该值的字段。

这是来自控制器的数组数据

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
                )
        )
);

    在控制器中我提出条件,

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

    但是cakephp显示错误,未知字段,

 这是发现,

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



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


    我还有其他任何方式可以创造出我想要的条件,

thankss before and after... T_T stuck  one week 

1 个答案:

答案 0 :(得分:1)

问题是您是在关联模型上应用条件而不是原始模型。解决此问题的一种方法是加入两个表。

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

这应该可以满足您的需求。

和平!的xD