Answer the question
In order to leave comments, you need to log in
How to build ActiveRecord queries in Yii2?
I read the manual, both this one and in the tutorial on github, but I still can’t figure out the technique for compiling queries on ActiveRecord
. For example, the query that was built into Yii2 right after installation works:
$countries = $query->orderBy('name')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
$countries = $query->orderBy('name')
->all();
PHP Warning - yii\base\ErrorException
htmlspecialchars() expects parameter 1 to be string, array given
$countries = $query->where(['country.code' => 'AU'])->count();
$countries = $query->where(['country.population' > 4444444])->all();
Invalid Parameter - yii\base\InvalidParamException
Operator ''requires two operands.
$countries = $query->where(['>', 'country.population', 4444444])->all();
PHP Warning – yii\base\ErrorException
htmlspecialchars() expects parameter 1 to be string, array given
Answer the question
In order to leave comments, you need to log in
It's a Query Builder, not an ActiveRecord.
Get yourself an IDE like PhpStorm, and drop into the method on click on it, and see what arguments it takes and how it uses them. And so the documentation clearly enough describes how to use this functionality.
You can also use:
$countries = $query->orderBy('name')
->offset($pagination->offset)
->limit($pagination->limit)
->createCommand()->rawSql;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question