【问题标题】:HABTM with join CakephpHABTM 与加入 Cakephp
【发布时间】:2016-10-18 18:59:38
【问题描述】:

你好,我有一个表名University_Industry。它有uni_idind_id 作为University 表和Industry 表的外键。所以意味着大学会有很多行业,行业会有很多大学。我通过运行此查询成功获得了结果

public function getAllUniv(){
    return $this->find('all', array(
        'order' => 'uni_id',
        ));
     }

此查询返回大学及其行业。结果如下所示

Array
(
    [0] => Array
        (
            [University] => Array
                (
                    [uni_id] => 1
                    [uni_name] => NYC
                )

            [Industry] => Array
                (
                    [0] => Array
                        (
                            [ind_id] => 1
                            [ind_name] => Finance
                            [UniversityIndustry] => Array
                                (
                                    [id] => 1
                                    [uni_id] => 1
                                    [ind_id] => 1
                                )

                        )

                    [1] => Array
                        (
                            [ind_id] => 2
                            [ind_name] => Accounting
                            [UniversityIndustry] => Array
                                (
                                    [id] => 2
                                    [uni_id] => 1
                                    [ind_id] => 2
                                )

                        )

                )

        )

现在我有另一个名为 users 的表,其中 uni_id 也作为外键。所以我正在寻找一个查询,它只能获取那些在用户表中找到的行业的大学。所以它应该只获取那些 uni_id 存在于 users 表中的大学

我不知道如何在查询中加入 users 表并获得结果。

这就是我现在的大学模式的样子

class University extends AppModel
{   

    public $useTable = 'university';    
    public $primaryKey = 'uni_id';

    public $hasAndBelongsToMany = array(
    'Industry' =>
        array(
            'className'              => 'Industry',
            'joinTable'              => 'university_industry',
            'foreignKey'             => 'uni_id',
            'associationForeignKey'  => 'ind_id',
            'unique'                 => true,
            'conditions'             => '',
            'fields'                 => '',
            'order'                  => '',
            'limit'                  => '',
            'offset'                 => '',
        ),
       );
    public function getAllUniv(){
    return $this->find('all', array(
        'order' => 'uni_id',
        ));
     }
}

【问题讨论】:

    标签: cakephp cakephp-2.0 cakephp-2.3


    【解决方案1】:

    您可以在查找查询中加入数据。

    $this->find('all', array(
        'order' => 'uni_id',
        'joins' => array(  
            array(
            'table' => 'users ',
            'alias' => 'UserJoin',
            'type' => 'inner',
            'conditions' => array(
                'UserJoin.uni_id = University.id'
                )
            ),
        ));
    

    这里是official doc。希望对你有帮助

    【讨论】:

    • 非常感谢您的回答。我可以在用户表中按此处分组吗?
    • 是的,你可以像'group'=>array('UserJoin.uni_id')一样分组
    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多