Queries
Because
yii\db\ActiveQueryextends fromyii\db\Query, you can use all query building methods and query methods.
Get a single record:
$customer = Customer::find()
->where(['id' => 123])
->one();
Get many records:
$customers = Customer::find()
->where(['status' => Customer::STATUS_ACTIVE])
->orderBy('id')
->all();
Relational queries:
$orders = $customer->getOrders()
->where(['>', 'subtotal', 200])
->orderBy('id')
->all();
Using OR operator:
$events_tba_active = Event::find()
->where(['makes_sense' => 1, 'type' => Event::TYPE_WITHOUT_DATE])
->andWhere(['OR', ['<=', 'date', new Expression('CURDATE()')], ["date" => null]])
->orderBy('date ASC')
->all();
Check if a value is null:
$query->andFilterWhere(['is', 'hourly_range', new Expression('null')]);
Another variant of specifying conditions:
Job::deleteAll("created_timestamp < $time_border AND hit != 1");
More complex example:
// DELETE FROM job WHERE publication_timestamp < $time_border AND (starred = 0 OR (starred = 1 AND viewed = 1) )
return Job::deleteAll(
['AND', ['<', 'publication_timestamp', $time_border], ['OR', ['starred' => 0], ['starred' => 1, 'viewed' => 1]]]
);
Andrew Dorokhov