deez
deez

Reputation: 146

Yii Active Record adding multiple conditions in named scopes from different tables

I am trying to make a condition that the client ID must match the client ID and a region field in the locations table must match the region attached to the location id that is associated with the users location.

So I have 3 tables. The current table(t1), the table relation1 refers to (t2), and the table that t2 refers to in relation2 (we will call this t3).


    $this->getDbCriteria()->mergeWith(array(
        'with' => $rel,
        'condition'=>'relation1.client_id=:client_id AND
                      relation1.relation2.region=:region',
        'params'=>array(':client_id'=>$client_id, ':region'=>$region),
    ));

return $this;

Relation1 is the relation in the table once removed from this one. The table that relation1 refers to has a relation called relation2 that gets me to where I need to retrieve the region value.

If I remove the second condition and parameter it works, so I know relation1 is working, and relation2 also works in other contexts. Here they are if would like to see them.


    public function relations()
    {
        return array(
            'relation1' => array(self::BELONGS_TO, 't2', 't2_id'),
        );
    }

and


    public function relations()
    {
        return array(
            'relation2' => array(self::BELONGS_TO, 't3', 't3_id'),
             );
    } 

I really feel like this should work. Any help?

Upvotes: 3

Views: 2331

Answers (1)

deez
deez

Reputation: 146

I got the answer from another site. If you are interested, the solution is:


    $this->getDbCriteria()->mergeWith(array(
            'with' => array(
                'relation1' => array(
                       'condition' => 'relation1.column = :column_id', 
                       'params' => array(':column_id'=>$column_id)),
                'relation1.relation2' => array(
                       'condition' => 'relation2.column2 = :column2', 
                       'params' => array(':column2'=>$column2))
            ),
        ));
    return $this;

Thanks for your help!

Upvotes: 3

Related Questions