yii2: YII db bug in postgres with array values

What steps will reproduce the problem?

$query = (new \yii\db\Query())
            ->from('table')
            ->where(['=', 'field', [1]])
            ->count();

If values is an array, containing only 1 element, the query will not work (always returns 0), although createCommand()->getRawSql() returns the correct query. If I have 2 or more elements in values, then query works fine (it is executed as IN query).

What is the expected result?

According to documentation http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail In case when a value is an array, an IN expression will be generated. But it doesn’t happen. Moreover I expect the results to be returned, but I always get 0 in all cases.

What do you get instead?

I get empty results, the IN statement is not generated, and getRawSql() shows wrong query.

Additional info

Q A
Yii version 2.0.12
PHP version 7.0.14
Operating system Debian 8
Database postgres 9.5

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Reproduced:

public function testCompareArray()
    {
        $connection = $this->getConnection();
        $query = new Query();
        $query->from('item')
            ->where(['=', 'id', [2]]);
        $result = $query->all($connection);
        $this->assertCount(1, $result);
        $this->assertEquals(2, $result[0]['id']);
    }

Triggers notice: ’ Undefined offset: 1’

If you wish IN statement to be generated you should use in operator instead of =:

$query = (new \yii\db\Query())
            ->from('table')
            ->where(['in', 'field', [1]])
            ->count();