The returned
ActiveQueryInterface instance can be further customized by calling
methods defined in
ActiveQueryInterface before
one() or
all() is called to return
populated ActiveRecord instances. For example,
php
find the customer whose ID is 1
$customer = Customer::find()->where(['id' => 1])->one();
find all active customers and order them by their age:
$customers = Customer::find()
->where(['status' => 1])
->orderBy('age')
->all();
This method is also called by [[BaseActiveRecord::hasOne()]] and [[BaseActiveRecord::hasMany()]] to
create a relational query.
You may override this method to return a customized query. For example,
php
class Customer extends ActiveRecord
{
public static function find()
{
use CustomerQuery instead of the default ActiveQuery
return new CustomerQuery(get_called_class());
}
}
The following code shows how to apply a default condition for all queries:
php
class Customer extends ActiveRecord
{
public static function find()
{
return parent::find()->where(['deleted' => false]);
}
}
Use andWhere()/orWhere() to apply the default condition
SELECT FROM customer WHERE deleted`=:deleted AND age>30
$customers = Customer::find()->andWhere('age>30')->all();
Use where() to ignore the default condition
SELECT FROM customer WHERE age>30
$customers = Customer::find()->where('age>30')->all();