Copy Yii\db\query

All,

I’m trying to clone an instance of Query to be used in a subsequent union like below.




            $unionQuery = clone $meatQuery;

            $unionQuery->groupBy(["c.address"]);

            $unionQuery->addSelect([(new \yii\db\Expression('2 as sort_col'))]);


            $meatQuery->addSelect([(new \yii\db\Expression('1 as sort_col'))]);

            $meatQuery->filterWhere(["v.vendor" => $queryParams["vendor"]]);

            $meatQuery->union($unionQuery);



This union is conditional, so I don’t want to do it every time and I didn’t feel like creating the huge sql for this query again (query is basically the same, except the union shouldn’t include the vendor restriction).

What’s interesting is the union object retains the f.store where restriction, but the actual meatQuery loses it. Here’s the $meatQuery before and after the union. I put … on potentially sensitive information.

Before




yii\db\Query#1

(

    [select] => [

        ...

    ]

    [selectOption] => null

    [distinct] => null

    [from] => [

        0 => 'ftw011 f'

    ]

    [groupBy] => null

    [join] => [

        0 => [

            0 => 'LEFT JOIN'

            1 => 'vendors v'

            2 => 'f.plu = v.plu'

        ]

        1 => [

            0 => 'LEFT JOIN'

            1 => 'customers c'

            2 => 'f.id = c.id'

        ]

        2 => [

            0 => 'LEFT JOIN'

            1 => 'stores s'

            2 => 'f.store = s.id'

        ]

    ]

    [having] => null

    [union] => null

    [params] => []

    [yii\base\Component:_events] => []

    [yii\base\Component:_behaviors] => null

    [where] => [

        0 => 'and'

        1 => 'f.id IS NOT NULL'

        2 => [

            'f.store' => [

                0 => '122'

            ]

        ]

    ]

    [limit] => null

    [offset] => null

    [orderBy] => [

        'f.store' => 4

        'weighted' => 3

    ]

    [indexBy] => null

)



After




yii\db\Query#1

(

    [select] => [

        ...

        13 => yii\db\Expression#2

        (

            [expression] => '1 as sort_col'

            [params] => []

        )

    ]

    [selectOption] => null

    [distinct] => null

    [from] => [

        0 => 'ftw011 f'

    ]

    [groupBy] => null

    [join] => [

        0 => [

            0 => 'LEFT JOIN'

            1 => 'vendors v'

            2 => 'f.plu = v.plu'

        ]

        1 => [

            0 => 'LEFT JOIN'

            1 => 'customers c'

            2 => 'f.id = c.id'

        ]

        2 => [

            0 => 'LEFT JOIN'

            1 => 'stores s'

            2 => 'f.store = s.id'

        ]

    ]

    [having] => null

    [union] => [

        0 => [

            'query' => yii\db\Query#3

            (

                [select] => [

                    ...

                    13 => yii\db\Expression#4

                    (

                        [expression] => '2 as sort_col'

                        [params] => []

                    )

                ]

                [selectOption] => null

                [distinct] => null

                [from] => [

                    0 => 'ftw011 f'

                ]

                [groupBy] => [

                    0 => 'c.address'

                ]

                [join] => [

                    0 => [

                        0 => 'LEFT JOIN'

                        1 => 'vendors v'

                        2 => 'f.plu = v.plu'

                    ]

                    1 => [

                        0 => 'LEFT JOIN'

                        1 => 'customers c'

                        2 => 'f.id = c.id'

                    ]

                    2 => [

                        0 => 'LEFT JOIN'

                        1 => 'stores s'

                        2 => 'f.store = s.id'

                    ]

                ]

                [having] => null

                [union] => null

                [params] => []

                [yii\base\Component:_events] => []

                [yii\base\Component:_behaviors] => null

                [where] => [

                    0 => 'and'

                    1 => 'f.id IS NOT NULL'

                    2 => [

                        'f.store' => [

                            0 => '122'

                        ]

                    ]

                ]

                [limit] => null

                [offset] => null

                [orderBy] => [

                    'f.store' => 4

                    'weighted' => 3

                ]

                [indexBy] => null

            )

            'all' => false

        ]

    ]

    [params] => []

    [yii\base\Component:_events] => []

    [yii\base\Component:_behaviors] => null

    [where] => [

        'v.vendor' => [

            0 => '817230'

        ]

    ]

    [limit] => null

    [offset] => null

    [orderBy] => [

        'f.store' => 4

        'weighted' => 3

    ]

    [indexBy] => null

)

Notice the query lost one of the f.store restrictions in the [where] array. I’d rather not have to double check and see if my where statements are holding up in the union and have to re-ask the questions and insert the where again. Why does the union get rid of that array element / what’s the appropriate way to handle this?

Thanks

It was due to my own stupidity. Notice $meatQuery->filterWhere … should be andFilterWhere.




            $unionQuery = clone $meatQuery;

            $unionQuery->groupBy(["c.address"]);

            $unionQuery->addSelect([(new \yii\db\Expression('2 as sort_col'))]);


            $meatQuery->addSelect([(new \yii\db\Expression('1 as sort_col'))]);

            $meatQuery->filterWhere(["v.vendor" => $queryParams["vendor"]]); //sigh... 

            $meatQuery->union($unionQuery);