fuel / orm

Fuel PHP Framework - Fuel v1.x ORM
http://fuelphp.com/docs/packages/orm/intro.html
151 stars 95 forks source link

Related conditions are applied to incorrect place in the query #449

Closed paullb514 closed 3 years ago

paullb514 commented 3 years ago

With the below query with teh is_visible check inside the related statement

\Model_A::query()->related('B',array('where'=>array(array('is_visible',true))))->get();

Expected result (simplifying select select * from A left join B on (A.id = B.A_id and B.is_visible = true)

Actual Result select * from A left join B on (A.id = B.A_id) where B.is_visible = true

These are two different queries and as the condition is being specified in the related statment and hence inside the left join clause. It definitely shouldn't be tacked on the end of the query like this.

WanWizard commented 3 years ago

You are right the two are different, but you've asked for a where, so you got it. This is intended behavior, and equivalent to

\Model_A::query()->related('B')->where('B.is_visible', '=' ,true)->get(); 

What you want is

\Model_A::query()->related('B',array('join_on'=>array(array('is_visible',\DB::expr(true)))))->get(); 

Note that the DB::expr() is needed on literals because the default for joins are column names which are not quoted.

paullb514 commented 3 years ago

Thank you for that, I was able to confirm that the join_on worked. I'd still maintain putting the where condition inside the related parameter should put the condition on the join and not on the query as whole from an intuitive sense and be functionally equivalent to the "join_on" but that definitely doesn't rise to the level of a bug so I'll go ahead and close this out.

WanWizard commented 3 years ago

You have a point.

But changing it will break 11 years worth of code out there, which is why it was decided to make "join_on" public (it was already used internally to construct the join on primary key).