如何在 hasMany 關聯中使用 CakePHP 2 查找? (How to use CakePHP 2 find in a hasMany association?)


問題描述

如何在 hasMany 關聯中使用 CakePHP 2 查找? (How to use CakePHP 2 find in a hasMany association?)

I know this is an old quastion, but I just can't seem to make It work.

I have these relationships: User hasMany Responsible Responsible belongsTo User

I need, through my User model, perform a find like this:

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

But this won't work, It will throw a "Unknown column 'Responsible.email' in where clouse".

my real code is like this:

    $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));

I have searched and tried in many ways, but I just can't make It work, how do I do this?

Edit:

I was able to do It with containable, but it also returns User with empty Responsible...


參考解法

方法 1:

Got It:

       $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);

Works well :)

(by Felipe ZavanFelipe Zavan)

參考文件

  1. How to use CakePHP 2 find in a hasMany association? (CC BY-SA 3.0/4.0)

#has-many #find #cakephp #search #Relationship






相關問題

Ruby on Rails - Has_many,批量分配問題 (Ruby on Rails - Has_many, Mass Assignment issue)

如何在 hasMany 關聯中使用 CakePHP 2 查找? (How to use CakePHP 2 find in a hasMany association?)

模型沒有像我認為的那樣連接 (Models not connecting as I think they should be)

CakePhp find không tìm nạp hasMany mối quan hệ (CakePhp find doesn't fetch hasMany relationships)

創建關聯後,控制器中的 Ruby on rails NoMethodError (Ruby on rails NoMethodError in controller after create an association)

是否可以以兩種不同的方式關聯模型? (Is it possible to associate models in two different ways?)

在 DetailView yii2 中顯示 HAS_MANY 關係數據 (Display HAS_MANY relation data in DetailView yii2)

Rails - 檢查 has_many 關聯中是否存在記錄 (Rails - check if record exists in has_many association)

在 Rails 中,我可以在另一個模型的模型上設置 has_many 關係嗎? (In Rails, can I set a has_many relation on a model from another model?)

從大於 0 開始計數器緩存 (Starting a counter cache from greater than 0)

如何使用 has_many 關聯在 Rails 中排序? (How to sort in rails with has_many association?)

如何將 ActiveModel::Serializer :has_many 關聯的密鑰格式(即駝峰式)指定為一次性選項(不是全局配置)? (How to specify the key format (i.e. camelcase) for an ActiveModel::Serializer :has_many association as a one-off option (not global config)?)







留言討論