Commit 32365e81 by Alexander Makarov

Fixes #2805

parent 772f56f0
...@@ -76,12 +76,13 @@ There are two ActiveRecord methods for querying data from database: ...@@ -76,12 +76,13 @@ There are two ActiveRecord methods for querying data from database:
- [[yii\db\ActiveRecord::find()]] - [[yii\db\ActiveRecord::find()]]
- [[yii\db\ActiveRecord::findBySql()]] - [[yii\db\ActiveRecord::findBySql()]]
Both methods return an [[yii\db\ActiveQuery]] instance, which extends [[yii\db\Query]], and thus supports the same set of flexible and powerful DB query methods. The following examples demonstrate some of the possibilities. Both methods return an [[yii\db\ActiveQuery]] instance, which extends [[yii\db\Query]], and thus supports the same set
of flexible and powerful DB query methods. The following examples demonstrate some of the possibilities.
```php ```php
// to retrieve all *active* customers and order them by their ID: // to retrieve all *active* customers and order them by their ID:
$customers = Customer::find() $customers = Customer::find()
->where(['status' => $active]) ->where(['status' => Customer::STATUS_ACTIVE])
->orderBy('id') ->orderBy('id')
->all(); ->all();
...@@ -99,7 +100,7 @@ $customers = Customer::findBySql($sql)->all(); ...@@ -99,7 +100,7 @@ $customers = Customer::findBySql($sql)->all();
// to return the number of *active* customers: // to return the number of *active* customers:
$count = Customer::find() $count = Customer::find()
->where(['status' => $active]) ->where(['status' => Customer::STATUS_ACTIVE])
->count(); ->count();
// to return customers in terms of arrays rather than `Customer` objects: // to return customers in terms of arrays rather than `Customer` objects:
...@@ -113,6 +114,10 @@ $customers = Customer::find()->indexBy('id')->all(); ...@@ -113,6 +114,10 @@ $customers = Customer::find()->indexBy('id')->all();
// $customers array is indexed by customer IDs // $customers array is indexed by customer IDs
``` ```
> Note: In the code above `Customer::STATUS_ACTIVE` is a constant defined in the class itself. It is a good practice to
use such approach instead of relying on hardcoded strings and numbers directly.
Batch query is also supported when working with Active Record. For example, Batch query is also supported when working with Active Record. For example,
```php ```php
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment