2016-02-01 176 views
0

不好意思,大家在cakephp有很高的技巧, 我開始在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 

+0

您已將此'ClaimDetail.date'更改爲'Claim.date' –

回答

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

+1

非常感謝您,我將在稍後嘗試您的代碼..比我會告訴您結果.. –