0

我知道这是一个古老的问题,但我似乎无法让它发挥作用。

我有这些关系:
User hasMany Responsible
Responsible belongsTo User

我需要通过我的 User 模型执行这样的查找:

$users = $this->User->find('all', array('conditions' => array('Responsible.email' => $responsible_email));

但这不起作用,它会在哪里抛出一个“未知列'Responsible.email'”。

我的真实代码是这样的:

    $conditions = array();

    if (!empty($params['filter_by_name'])) {
        $conditions[] .= "LOWER(CONCAT(Profile.name, ' ', Profile.surname)) LIKE '%".$params['filter_by_name']."%'";
    }

    if (!empty($params['filter_by_email'])) {
        $conditions[] .= "LOWER(User.email) = '".$params['filter_by_email']."'";
    }

    if (!empty($params['filter_by_responsible_email'])) {
        $conditions[] .= "LOWER(Responsible.email) = '".$params['filter_by_responsible_email']."'";
    }

    $results = $this->User->find('all', array('conditions' => $conditions));

我已经搜索并尝试了很多方法,但我无法让它工作,我该怎么做?

编辑:

我能够用可包含的方式做到这一点,但它也返回带有空责任的用户......

4

1 回答 1

1

知道了:

       $options['joins'] = array(
            array(
                'table' => 'responsibles',
                'alias' => 'Responsible',
                'type' => 'INNER',
                'conditions' => array("User.id = Responsible.user_id")
            )
        );

        $conditions[] .= "Responsible.email = '".$params['filter_by_responsible_email']."'";

        $options['conditions'] = $conditions;

        $results = $this->User->find('all', $options);

效果很好 :)

于 2012-08-13T00:36:31.227 回答