cakephp 在具有关系模型的模型中访问字段搜索
cakephp access field search in model that have relation model
各位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
问题是您在关联模型而不是原始模型上应用条件。解决这个问题的一种方法是连接两个表。
$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
各位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
问题是您在关联模型而不是原始模型上应用条件。解决这个问题的一种方法是连接两个表。
$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