active-record icon indicating copy to clipboard operation
active-record copied to clipboard

joinWith and with using onCondition has problem with eager or lazy

Open gholizadeh opened this issue 6 years ago • 4 comments

What steps will reproduce the problem?

I want select all failures with related job only if it has a reason on failure table and I use this

$failures = Failure::find()
        ->joinWith([
            'job' => function ($query) {
                $query->andOnCondition(['failure.reason' => 1]);
            }
])->all();

Error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'failure.reason' in 'where clause' SELECT * FROM job WHERE (job_id=2) AND (failure.reason=1)

Additional info

I have used (with) and (joinWith) also used (true and false) as second param to indicate eager or lazy and always get same error

Q A
Yii version 2.0

gholizadeh avatar Mar 30 '19 15:03 gholizadeh

also I found svit-ws commented on a same issue 2 years ago.

gholizadeh avatar Mar 30 '19 15:03 gholizadeh

Is there any reason why you're not using:

$failures = Failure::find()
        ->andWhere(['failure.reason' => 1])
        ->joinWith(['job'])
        ->all();

?

rob006 avatar Mar 30 '19 21:03 rob006

@rob006 because as I said I want all failures and the job (but I need the job only if failure has reason). it will gives the failures have reason not all failures

gholizadeh avatar Mar 31 '19 07:03 gholizadeh

It would be grate if andOnCondition and OnCondition had another param that indicate the scope of condition (in join as a general condition or in joined table which is considered by default and really useful in most of the times)

gholizadeh avatar Mar 31 '19 07:03 gholizadeh