Added count, average, sum, min, max, scalar methods to ActiveQuery.

Added support for scopes defined in AR classes.
This commit is contained in:
Qiang Xue
2013-01-17 19:26:20 -05:00
parent 63bb6efb6e
commit b51d347465
5 changed files with 110 additions and 46 deletions

View File

@ -57,11 +57,10 @@ return array(
### Getting Data from Database
There are three ActiveRecord methods for getting data:
There are two ActiveRecord methods for getting data:
- [[find()]]
- [[findBySql()]]
- [[count()]]
They all return an [[ActiveQuery]] instance. Coupled with the various customization and query methods
provided by [[ActiveQuery]], ActiveRecord supports very flexible and powerful data retrieval approaches.
@ -88,9 +87,9 @@ $sql = 'SELECT * FROM tbl_customer';
$customers = Customer::findBySql($sql)->all();
// to return the number of *active* customers:
$count = Customer::count()
$count = Customer::find()
->where(array('status' => $active))
->value();
->count();
// to return customers in terms of arrays rather than `Customer` objects:
$customers = Customer::find()->asArray()->all();
@ -222,10 +221,10 @@ subtotal exceeds certain amount:
~~~
class Customer extends \yii\db\ActiveRecord
{
public function getBigOrders()
public function getBigOrders($threshold = 100)
{
return $this->hasMany('Order', array('customer_id' => 'id'))
->where('subtotal > 100')
->where('subtotal > :threshold', array(':threshold' => $threshold))
->orderBy('id');
}
}