From a948e7387231ee49d0664c114c40a9cce7b41eb8 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sun, 5 May 2013 01:27:13 +0400 Subject: [PATCH] example for using asArray --- docs/guide/performance.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/guide/performance.md b/docs/guide/performance.md index 58e8258..9a871dc 100644 --- a/docs/guide/performance.md +++ b/docs/guide/performance.md @@ -147,9 +147,28 @@ A good way to save memory and processing time on read-only pages is to use ActiveRecord's `asArray` method. ```php +class PostController extends Controller +{ + public function actionIndex() + { + $posts = Post::find()->orderBy('id DESC')->limit(100)->asArray()->all(); + echo $this->render('index', array( + 'posts' => $posts, + )); + } +} ``` -TBD +In the view you should access fields of each invidual record from `$posts` as array: + +```php +foreach($posts as $post) { + echo $post['title']."
"; +} +``` + +Note that you can use array notation even if `asArray` wasn't specified and you're +working with AR objects. ### Processing data in background