我需要从 Assurances_indicator_data 获取数据(与该指标数据相关的所有保证 ID),并且我不知道在 CakePHP 中加入数据的正确方法,我的代码不起作用,它返回一个空数组。
指标基准模型:
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;
}
}
最佳答案
在 CakePhp 中连接表的最简单方法是对代表数据库中的表的每个表进行建模。 下面的示例,您可以将其修改为您的表和模型的名称:
class ModelOne extends AppModel{
public $belongTo = array('ModelTwo');
}
class ModelTwo extends AppModel{
public $hasMany = array('ModelOne');
}
然后在您的 Controller 中,如果您查询这些模型中的任何一个,您的响应数组应包含两个表中的数据。
关于cakephp - 从 CakePHP 中的连接表获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30506428/