2015-05-28 26 views
2

我需要從assurances_indicator_data獲取數據(與該指標數據相關的所有保證的id),並且我不知道在CakePHP中連接數據的正確方法, t工作,它返回一個空數組。從CakePHP中的連接表中獲取數據

IndicatorDatum型號:

public $hasAndBelongsToMany = array(
     'Assurance' => array(
      'className' => 'Assurance', 
      'joinTable' => 'assurances_indicator_data', 
      'foreignKey' => 'indicator_datum_id', 
      'associationForeignKey' => 'assurance_id', 
      'unique' => true, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'finderQuery' => '', 
      'deleteQuery' => '', 
      'insertQuery' => '' 
     ) 
    ); 


function getIndicatorDataAssurance($indi_id){ 
      $assuranceData = $this->find('all',array(
       'joins' => array(
        array('table' => 'assurances_indicator_data', 
          'type' => 'INNER', 
         'conditions' => array(
        'AssurancesIndicatorData.indicator_datum_id' => $cun_indi))))); 


      return $assuranceData; 


      } 


} 

回答

2

連接表在CakePHP最簡單的方法是讓每個模型代表你的數據庫中的表。 下面一個例子,你可以將它修改爲你的表和模型的名稱:

class ModelOne extends AppModel{ 
    public $belongTo = array('ModelTwo'); 

} 


class ModelTwo extends AppModel{ 
    public $hasMany = array('ModelOne'); 

} 

然後在您的控制器如果查詢這些模型,你的反應陣列應該包含兩個表中的數據。