db icon indicating copy to clipboard operation
db copied to clipboard

andHaving must be similar to andWhere. And are nested

Open ruskid opened this issue 6 years ago • 1 comments

What steps will reproduce the problem?

Try to add several addHavings to the query.

What is the expected result?

 [having] => [
        0 => 'and'
        1 => [
            0 => '<'
            1 => 'miles'
            2 => 100
        ]
        2 => [
            0 => '<='
            1 => 'finalPrice * ((100 - finalDiscount) / 100)'
            2 => 'test'
        ]
        3 => [
            0 => '<='
            1 => 'finalPrice * ((100 - finalDiscount) / 100)'
            2 => 'tests'
        ]
    ]

What do you get instead?

[having] => [
        0 => 'and'
        1 => [
            0 => 'and'
            1 => [
                0 => '<'
                1 => 'miles'
                2 => 100
            ]
            2 => [
                0 => '<='
                1 => 'finalPrice * ((100 - finalDiscount) / 100)'
                2 => 'test'
            ]
        ]
        2 => [
            0 => '<='
            1 => 'finalPrice * ((100 - finalDiscount) / 100)'
            2 => 'tests'
        ]
    ]

Additional info

Q A
Yii version 2.0.12

Solution?

andHaving must be same as andWhere. Instead of :

if ($this->having === null) {
            $this->having = $condition;
        } else {
            $this->having = ['and', $this->having, $condition];
        }
        $this->addParams($params);
        return $this;

It should be like this:

if ($this->having === null) {
            $this->having = $condition;
        } elseif (is_array($this->having) && isset($this->having[0]) && strcasecmp($this->having[0], 'and') === 0) {
            $this->having[] = $condition;
        } else {
            $this->having = ['and', $this->having, $condition];
        }
        $this->addParams($params);
        return $this;

ruskid avatar Aug 03 '17 20:08 ruskid

it does not change anything in functionalilty, it just generates nicer SQL.

cebe avatar Aug 14 '17 11:08 cebe