Skip to content
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Mongo_Collection

colinmollenhour edited this page Sep 13, 2010 · 3 revisions

This class can be used in any of the following ways:

1. Directly as a wrapper for MongoCollection/MongoCursor:

$posts = new Mongo_Collection('posts');
$posts->sort_desc('published')->limit(10)->as_array(); // array of arrays

2. As part of the Table Data Gateway pattern

class Model_Post extends Mongo_Document {
  protected $name = 'posts';
  // All model-related code here
}
$posts = Mongo_Document::factory('post')->collection(TRUE);
$posts->sort_desc('published')->limit(10)->as_array(); // array of Model_Post

3. As part of the Row Data Gateway pattern:

class Model_Post_Collection extends Mongo_Collection {
  protected $name = 'posts';
  // Collection-related code here
}
class Model_Post extends Mongo_Document {
  // Document-related code here
}
$posts = Mongo_Document::factory('post')->collection(TRUE);
$posts->sort_desc('published')->limit(10)->as_array(); // array of Model_Post

The collection instance allows query results to be accessed as an iterator of models rather than arrays when method 2 or 3 is used:

foreach($posts as $post) {
  echo "{$post->title}\n";
}
Clone this wiki locally