如何根据相关模型中的字段查找模型

时间:2013-12-30 15:45:24

标签: php mysql sql cakephp

我正在使用cakephp_2_3_6。 我有三个模型:用户,组和城市。

集团有很多用户。 城市有很多用户。 用户属于集团和城市。

  

group =>数组(
id,
groupName

     

city =>数组(
id,
cityName,zipCode

     

用户=>数组(
id,
userName,
group_id,
city_id

我想向可以根据用户所在群组的groupName或依赖于这些用户所居住的cityName找到用户的人提供搜索功能。

    public function search(){  
        $url = $this->params['url'];  
        $where = $url['where'];  
        $group = $url['group'];  
        if(!$where && !$group){
            $this->set('foundItems', $this->User->find('all'));
        }elseif(!$group && $where){         
            $this->set('foundItems', $this->User->find('all', array('conditions' => array('User.cityName' => $where))));
        }elseif($group && !$where){         
            $this->set('foundItems', $this->User->find('all', array('conditions' => array('User.groupName' => $group))));
        }else{          
            $this->set('foundItems', $this->User->find('all', array('conditions' => array('User.groupName' => $group), 'conditions' => array('User.cityName' => $where))));
        }

正如您在上面的代码中看到的,我使用URL从表单中传输cityName和groupName。 我尝试使用此代码执行不同的操作,例如使用city.conditions代替条件,或使用City.cityName代替cityNameUser.cityName。 根据CookBook JoinTable section“belongsTo”执行自动连接。所以我想它应该以某种方式起作用。

因为我尝试了很多不同的方法,在这个页面和CakePHP CookBook中阅读了很多内容并且没有找到解决方案,你可以帮助我运行这个搜索功能吗?

以下是用户模型

    <?php
        class User extends AppModel{
        public $useDbConfig = 'sites';
        public $belongsTo = array(  'City' => array(
                        'className' => 'City',
                        'foreignKey' => 'city_id'),
                    'Group' => array(
                        'className' => 'Group',
                        'foreignKey' => 'group_id')
            );

        public $validate = array(
            'userName' => array(
        'rule' => 'notEmpty'
             )
        );

        }
    ?>

2 个答案:

答案 0 :(得分:0)

我不明白你为什么在所有三个表中都有groupName。您确定发布了正确的数据库结构吗?

但也许你想要做的是:

public function search(){  
    $url = $this->params['url'];  
    $where = $url['where'];  
    $group = $url['group'];  
    if(!$where && !$group){
        $this->set('foundItems', $this->User->find('all'));
    }elseif(!$group && $where){         
        $this->set('foundItems', $this->User->find('all', array('conditions' => array('City.cityName' => $where))));
    }elseif($group && !$where){         
        $this->set('foundItems', $this->User->find('all', array('conditions' => array('Group.groupName' => $group))));
    }else{          
        $this->set('foundItems', $this->User->find('all', array('conditions' => array('Group.groupName' => $group, 'City.cityName' => $where))));
    }

(请注意我刚刚修改了conditions选项,如果你的逻辑有效,我没有加油)

答案 1 :(得分:0)

我终于明白了。
我想要做的关键是deeper associations下的ContainableBehavior描述了它是如何完成的。
在我的例子中,我必须改变这样的查找语句:

    //attach the behavior on the fly, this has to be done once and only with the main Model
    $this->User->Behaviors->load('Containable');
    ....
    $this->User->find('all', array('contain' => array('City' => array('conditions' => array('City.cityName' => $where)), 'Group')));

这样我就会寻找包含City和Group的用户模型。现在,我只查看City中名称与变量$ where。

的值匹配的用户

<强> EDID
解决方案并不完美,因为这样您将始终获得所有用户,如果他们被分配到符合城市也将返回的条件的城市。在上面的示例中,所有组都被返回 我想要的是:
仅限城市中符合此用户条件和组的用户 我如何得到我想要的东西:
然后我查看并使用join tables找到了解决方案。使用内部联接将仅返回城市中符合条件的用户 代码,如果您只有一个表的条件

    $this->User->City->recursive = -1;
    ....
    $options['joins'] = array(
            array('table' => 'cities',
            'alias' => 'City',
            'type' => 'inner',
            'conditions' => array(
                'User.city_id = City.id',
            )
            )
        );
        $options['conditions'] = array(
            'City.cityName' => $where
        );
        $result = $this->User->find('all', $options);

代码,如果您有两个表的条​​件

    $this->User->City->recursive = -1;
    $this->User->Group->recursive = -1;
    ....
    $options['joins'] = array(
            array('table' => 'cities',
            'alias' => 'City',
            'type' => 'inner',
            'conditions' => array(
                'User.city_id = City.id',
            )
            ),
           array('table' => 'groups',
            'alias' => 'Group',
            'type' => 'inner',
            'conditions' => array(
                'User.group_id = group.id',
            )
            )
        );
        $options['conditions'] = array( 
                'City.cityName' => $where,
            'Group.groupName' => $group
        );
        $result = $this->User->find('all', $options);