cakephp - 从 CakePHP 中的连接表获取数据

标签 cakephp join cakephp-2.0 has-and-belongs-to-many

我需要从 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/

相关文章:

php - CakePHP 站点主页的 Time to First Byte 很长

php - 如何在 CakePHP 2.4 中设置 AuthComponent 的数据库?

php - MySQL 查询返回表中未签名的内容不再有效

cakephp - Cakephp 中的 Paypal 集成

php - 错误: Call to undefined function SUM()

apache - 如何使子目录中的 cakePHP 2.x 出现在根目录中(在 htaccess 中使用 mod_rewrite)?

git - 使用 capistrano 和 git 将相同的代码库(略有差异)部署到多个服务器

mysql - 如何简化复杂的mysql全外连接

sql - Django左外连接过滤器

php - CakeEmail 如何在堆栈跟踪之前确定失败?