Yii2 : ActiveQuery Example and what is the reason to generate ActiveQuery class separately in Gii?
The Active Query represents a DB query associated with an Active Record class. It is usually used to override the default find()
method of a specific model where it will be used to generate the query before sending to DB :
class OrderQuery extends ActiveQuery
{
public function payed()
{
return $this->andWhere(['status' => 1]);
}
public function big($threshold = 100)
{
return $this->andWhere(['>', 'subtotal', $threshold]);
}
}
If you worked before with Yii 1 then this is what replaces Yii 1.x Named Scopes in Yii2. All you have to do is to override the find()
method in your model class to use the ActiveQuery class above :
// This will be auto generated by gii if 'Generate ActiveQuery' is selected
public static function find()
{
return new \app\models\OrderQuery(get_called_class());
}
Then you can use it this way :
$payed_orders = Order::find()->payed()->all();
$very_big_orders = Order::find()->big(999)->all();
$big_payed_orders = Order::find()->big()->payed()->all();
A different use case of the same ActiveQuery class defined above is by using it when defining relational data in a related model class like:
class Customer extends \yii\db\ActiveRecord
{
...
public function getPayedOrders()
{
return $this->hasMany(Order::className(),['customer_id' => 'id'])->payed();
}
}
Then you can eager load customers with their respective payed orders by doing :
$customers = Customer::find()->with('payedOrders')->all();
Yii2 - Count and sum not include of result of ActiveQuery
You shoudl add the pubblic var for bundles and pieces in your pipe model
(for recive the result of query )
class Pipe extends \yii\db\ActiveRecord
{
public $bundles;
public $pieces;
......
Yii2 ActiveQuery: is it possible to combine andWhere and orWhere?
Currently there is no straight way to join conditions of multiple queries. On GitHub there is already a feature request for this, but it is not implemented yet.
Currently you may try 2 solutions:
If you don't want to repeat conditions, you may create helper for them:
private function getExpiredContition() {
return ['<=', 'order.expiresAt', date('Y-m-d H:i:s')];
}
private function getCancelledContition() {
return ['NOT', ['order.cancelled' => null]];
}
public function expired() {
return $this->andWhere($this->getExpiredContition());
}
public function cancelled() {
return $this->andWhere($this->getCancelledContition());
}
public function archived() {
return $this->andWhere([
'OR',
$this->getExpiredContition(),
$this->getExpiredContition(),
]);
}You may try to access
$where
property directly and merge it in one query.public function archived() {
return $this->andWhere([
'OR',
(new static())->cancelled()->where,
(new static())->expired()->where,
]);
}
Yii2 - replacement for beforeFind to optimize nested views in MySql
About how to involve an ActiveQuery class check my answer here:
Yii2 : ActiveQuery Example and what is the reason to generate ActiveQuery class separately in Gii?
But if what you are trying to do doesn't require building named scopes then you may simply override the find method by something like this:
public static function find()
{
return parent::find()->where(['variable' => 'some value']);
}
Related Topics
How to Get the Last Path in a Url
What Is the Postman-Token Header Attribute in Generated Code from Postman
Get "Content-Type" Header of Request in PHP
Upload Images on Twitter Using PHP
Php: Date Larger Than Current Date
Can Anyone Get Access to My PHP Source Code
How to Install the Intl Extension for Twig
How to Check If Directory Contents Has Changed with PHP
Warning: "Continue" Targeting Switch Is Equivalent to "Break". Did You Mean to Use "Continue 2"
Disable Csrf in Laravel for Specific Route
Replace Deprecated Preg_Replace /E with Preg_Replace_Callback