S
S
SergioWhite222019-11-20 05:56:01
Yii
SergioWhite22, 2019-11-20 05:56:01

How to return results by ignoring empty sql conditions?

$result = Company::model()->with(
                'staffs',
                'staffs.phoneStaffs',
                'staffs.emailStaffs'
                )->findAll(array(
                    'condition'=> 'staffs.client_id=:idClientId '
                    . 'and phoneStaffs.client_id=:idClientId '
                    . 'and emailStaffs.client_id=:idClientId',
                    'params'=>  array(':idClientId' => $idClientId)));

With such a query, the result will be empty if no emailStaffs.client_id=:idClientId is found. How, then, to ignore the empty condition and return the result, but without the presence of emailStaffs in the resulting selection? Yii2 has filterWhere() for this case.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2019-11-25
@kimono

$result = Company::find()
  ->with(['staffs', 'staffs.phoneStaffs', 'staffs.emailStaffs'])
  ->filterWhere([
    'staffs.client_id' => $idClientId,
    'phoneStaffs.client_id' => $idClientId,
    'emailStaffs.client_id' => $idClientId,
  ])->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question