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


cakephp access field search in model that have relation model

对不起,所有在蛋糕方面有很高技能的人, 我是蛋糕的开始者, 我不知道如何访问我想使用该值的字段。

这是控制器显示的数组数据

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

但是蛋糕显示错误,未知字段,

这是发现,

$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 

问题是您将条件应用于关联的模型而不是原始模型。解决此问题的一种方法是连接两个表。

   $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