Comparator 'equals' In Querybuilder

There are many comparators in QueryBuilder, but I can’t find simple ‘equals’ (x = y).

In all tutorials on www [including doc-2.0] is this code to compare column and value:


$query->andFilterWhere(['something' => $someValue]);

And this code to compare with other comparators:


$query->andFilterWhere(['like', 'something', $someValue]);

When I try:


$query->andFilterWhere(['=', 'something', $someValue]);

or:


$query->andFilterWhere(['equals', 'something', $someValue]);

It throws exception.

How my code looks now:


class Xyz extends ActiveRecord {

...

	public static function fieldsSearchConfig() {

    	return [

    		'name' => 'like',

    		'license_id' => '='

			];

	}

 

	public function getActiveQuery($params) {

    	$activeQuery = WiatrakiX::find()->with('nazwaMiasta');

    	if (!($this->load($params) && $this->validate())) {

    		return $activeQuery;

    	}

    	

    	foreach(self::fieldsSearchConfig() as $fieldName => $fieldComparator) {

// BAD PART

    		if($fieldComparator == '=') {

    			$condition = [$fieldName => $this->$fieldName];

    		} else {

    			$condition = [$fieldComparator,  $fieldName, $this->$fieldName];

    		}

// BAD PART END

    		if(self::$searchType == self::SEARCH_AND) {

    			$activeQuery->andFilterWhere($condition);

    		} else {

    			$activeQuery->orFilterWhere($condition);

    		}

    	}

	}

}

How it should be:


class Xyz extends ActiveRecord {

...

	public static function fieldsSearchConfig() {

    	return [

    		'name' => 'like',

    		'license_id' => '='

			];

	}


	public function getActiveQuery($params) {

    	$activeQuery = WiatrakiX::find()->with('nazwaMiasta');

    	if (!($this->load($params) && $this->validate())) {

    		return $activeQuery;

    	}

    	

    	foreach(self::fieldsSearchConfig() as $fieldName => $fieldComparator) {

    		$condition = [$fieldComparator,  $fieldName, $this->$fieldName];

    		if(self::$searchType == self::SEARCH_AND) {

    			$activeQuery->andFilterWhere($condition);

    		} else {

    			$activeQuery->orFilterWhere($condition);

    		}

    	}

	}

}

Is there any way to make it works nice (and I’m just stupid/blind) or I must wait for Yii2 update?


Second question:

Are you going to add features from some Yii2 extensions [Editable Grid, DynaGrid] to final release or you want keep them as extensions?

Editable and dynagrid will be extensions.

Why don’t you like the following?


$query->andFilterWhere(['something' => $someValue]);

Of course it’s very useful to make code shorter ( [‘x’ => ‘y’] format) when you just want add some simple limitation for your query.

I think that there should be also possibility to use it with ‘3 arguments array format’ [‘equals’ comparator], because as you can see in my first post it’s not nice code when you need to generate filters dynamically.

EDIT:

Of course for me and you it’s very simple to understand what that code does and why, but in 2 months I will leave my current project and someone else will continue it. I’m sure he will ask himself “why is there that ‘if() {} else {}’, couldn’t he make it easier?”

OK, makes sense. Can you add it as an issue to github?

I’ve added: