Объеденить несколько сложных запросов

Общие вопросы по использованию второй версии фреймворка. Если не знаете как что-то сделать и это про Yii 2, вам сюда.
Ответить
garytopor
Сообщения: 113
Зарегистрирован: 2014.07.21, 22:23

Объеденить несколько сложных запросов

Сообщение garytopor »

Доброго времени суток!
Есть Условие:

Код: Выделить всё

<?
if ($this->image2_id != null) {
            if ($this->image2_id == Image2::FILTER_NO_IMAGES) {
                $query->andWhere(['NOT EXISTS', (new Query())->select('id')->from('{{%recipe_image}}')
                    ->andWhere('{{%recipe_image}}.imageable_id={{%restaurant}}.id')
                    ->andWhere(['{{%recipe_image}}.imageable_type' => Restaurant::class])]);

            } else if ($this->image2_id == Image2::FILTER_REJECTED_ONLY) {

                $queryNormalJoin = self::find()->select('{{%restaurant}}.id')->from('{{%restaurant}}')
                    ->joinWith('imagesWithoutCheckingClass')
                    ->andFilterWhere(['{{%recipe_image}}.imageable_type' => Restaurant::class]);

                $queryStatusNotRejected = self::find()->select('{{%restaurant}}.id')->from('{{%restaurant}}')
                    ->joinWith('imagesWithoutCheckingClass')
                    ->andFilterWhere(['{{%recipe_image}}.imageable_type' => Restaurant::class])
                    ->andWhere(['<>', '{{%recipe_image}}.image_status', ApproveStatus::rejected]);


                $query->joinWith('imagesWithoutCheckingClass')
                    ->andFilterWhere(['{{%recipe_image}}.imageable_type' => Restaurant::class])
                    ->andWhere(['{{%recipe_image}}.image_status' => ApproveStatus::rejected])
                    ->andWhere(['IN', '{{%restaurant}}.id', $queryNormalJoin])
                    ->andWhere(['NOT IN', '{{%restaurant}}.id', $queryStatusNotRejected]);

            } else if ($this->image2_id == Image2::FILTER_NOT_PUBLISHED_ONLY) {
                $queryStatusNotApproved = self::find()->select('{{%restaurant}}.id')->from('{{%restaurant}}')
                    ->joinWith('imagesWithoutCheckingClass')
                    ->andFilterWhere(['{{%recipe_image}}.imageable_type' => Restaurant::class])
                    ->andWhere(['<>', '{{%recipe_image}}.image_status', ApproveStatus::approved]);

                $queryStatusApproved = self::find()->select('{{%restaurant}}.id')->from('{{%restaurant}}')
                    ->joinWith('imagesWithoutCheckingClass')
                    ->andFilterWhere(['{{%recipe_image}}.imageable_type' => Restaurant::class])
                    ->andWhere(['=', '{{%recipe_image}}.image_status', ApproveStatus::approved]);

                $query->joinWith('imagesWithoutCheckingClass')
                    ->andFilterWhere(['{{%recipe_image}}.imageable_type' => Restaurant::class])
                    ->andWhere(['IN', '{{%restaurant}}.id', $queryStatusNotApproved])
                    ->andWhere(['NOT IN', '{{%restaurant}}.id', $queryStatusApproved]);
            } else if ($this->image2_id == Image2::FILTER_NOT_ALL_PUBLISHED) {
                $query->joinWith('imagesWithoutCheckingClass')
                    ->andFilterWhere(['{{%recipe_image}}.imageable_type' => Restaurant::class])
                    ->andWhere(['<>', '{{%recipe_image}}.image_status', ApproveStatus::approved]);
            } else if ($this->image2_id == Image2::FILTER_ALL_PUBLISHED) {
                $queryStatusNotApproved = self::find()->select('{{%restaurant}}.id')->from('{{%restaurant}}')
                    ->joinWith('imagesWithoutCheckingClass')
                    ->andFilterWhere(['{{%recipe_image}}.imageable_type' => Restaurant::class])
                    ->andWhere(['<>', '{{%recipe_image}}.image_status', ApproveStatus::approved]);

                $queryStatusApproved = self::find()->select('{{%restaurant}}.id')->from('{{%restaurant}}')
                    ->joinWith('imagesWithoutCheckingClass')
                    ->andFilterWhere(['{{%recipe_image}}.imageable_type' => Restaurant::class])
                    ->andWhere(['=', '{{%recipe_image}}.image_status', ApproveStatus::approved]);

                $query->joinWith('imagesWithoutCheckingClass')
                    ->andFilterWhere(['{{%recipe_image}}.imageable_type' => Restaurant::class])
                    ->andWhere(['NOT IN', '{{%restaurant}}.id', $queryStatusNotApproved])
                    ->andWhere(['IN', '{{%restaurant}}.id', $queryStatusApproved]);
            }
        }

Надо сделать из этого возможность для мультиселекта(массив) типо:

Код: Выделить всё

<?
if ($this->image2_id != null && count ($this->image2_id )>0) {
	    $images = array();
            $images[Image2::FILTER_NO_IMAGES] = ['NOT EXISTS', (new Query())->select('id')->from('{{%recipe_image}}'];

            $condition[] = 'OR';
            foreach($this->image2_id as $elem){
                $condition[] = $images[$elem];
            }
            $query->joinWith('recipe_image')->andWhere(
                $condition
             );
        }

Дело в том что не получается подключить joinWith динамически в условии [' ', ' ', ' ', ];


Как лучше сделать?
Ответить