Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

orOnCondition not set OR #6985

Closed
Tahiaji opened this issue Jan 23, 2015 · 6 comments
Closed

orOnCondition not set OR #6985

Tahiaji opened this issue Jan 23, 2015 · 6 comments

Comments

@Tahiaji
Copy link

Tahiaji commented Jan 23, 2015

I have two tables. For example user and engage(have 2 related fields user1,user2).

When I try write relation:
public function getEngages() {
return $this->hasMany(Engage::className(), ['user1' => 'id'])->orOnCondition(['user2' => $this->id]);
}
it generates code:
SELECT * FROM engage WHERE (user1=1) AND (user2=1)",

@cebe
Copy link
Member

cebe commented Jan 23, 2015

this is expected because onCondition is added to where using AND.

What you try to do here is a relation using two different attributes with OR. This is not possible to declare properly because eager loading and JOIN does not work in this case.
If you still want to apply the condition for the simple case of lazy loading you have to use orWhere().

@cebe cebe closed this as completed Jan 23, 2015
@fernandezekiel
Copy link
Contributor

sorry to comment on a closed issue but i want to know why the eager loading fails to recognize the orOnCondition to properly wrap the condition

@cebe
Copy link
Member

cebe commented Mar 7, 2015

@fernandezekiel relation definition has to be generic and not bound to a specific record like it is done here by passing $this->id. You can not turn this into an SQL query for eager loading.

The relation is defined using the fields user1 and id. This will give you a set of records that are fixed related records, onCondition is to further reduce this set of related records by additional conditions. It is not meant to replace or extend the relation definition thus it is added to the original relation condition with AND.

orOnCondition() in this case will add user2 = 1 condition to the exising onCondition. As there is none that will result in simply user2 = 1 to be the onCondition.

hope thats clear :)

@fernandezekiel
Copy link
Contributor

i have a slightly difference case, i'm not even passing $this->id
here's my relation

    public function getAuthRoles()
    {
        return $this->hasMany(AuthRole::className(), ['id' => 'auth_role_id'])->orOnCondition(['auth_role.title' => 'Authenticated'])->via('authRoleUsers');
    }

it produces the same kind of SQL as if i used andOnCondtion

@cebe
Copy link
Member

cebe commented Mar 7, 2015

this is normal because orOncondition will adjust the condition that is additinal to the condition that defines the relation. As you have not called onCondition before it, orOnCondition can not add anything.

return $this->hasMany(AuthRole::className(), ['id' => 'auth_role_id'])->onCondition(['a' => 'b'])->orOnCondition(['auth_role.title' => 'Authenticated'])->via('authRoleUsers');

Would give you a query like

JOIN ... ON id = auth_role_id AND (a= 'b' OR auth_role.title = 'Authenticated') 

@adampblack
Copy link

+1 Same issue as @fernandezekiel

There is no 'OR' option immediately after the initial where clause

e.g.
return $this->hasOne(Modelname::className(), [])->orOnCondition(['variable' => 'value']);

SELECT * FROM schema.tablename WHERE (0=1) AND "variable" = 'value'

Note:

orOnCondition produces AND

I want OR
e.g.

SELECT * FROM schema.tablename WHERE (0=1) OR "variable" = 'value'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants