3 class PostModel
extends ActiveMongo
11 const LIMIT_PER_PAGE
=20;
14 * Return this collection name.
18 function getCollectionName()
24 * Display the blog posts entries in the correct
25 * order, getting only the needed columns, and
28 * @return PostModel This document
30 function listing_page()
33 $collection = $this->_getCollection();
35 /* Deal with MongoDB directly */
40 'author_username' => 1,
43 $cursor = $collection->find(array(), $columns);
44 $cursor->sort(array("ts" => -1))->limit(self
::LIMIT_PER_PAGE
);
47 if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0) {
48 $skip = (int)$_GET['page'] * self
::LIMIT_PER_PAGE
;
52 /* Pass our results to ActiveMongo */
53 $this->setCursor($cursor);
58 function pre_save($op, &$obj)
60 if ($op == "create") {
61 $obj['ts'] = new MongoTimestamp();
69 * Check if we save the Author _id,
70 * or it throws an exception.
72 * @param mixed &$value Current value
73 * @param mixed $old_value Past value, used on Update
77 function author_filter(&$value, $old_value)
79 if (!$value instanceof MongoID
) {
80 if (!$value InstanceOf AuthorModel
) {
81 throw new Exception("The author property must be an AuthorModel object");
83 $value = $value->getID();
84 if ($value === false) {
85 throw new Exception("The AuthorModel doesn't have any record");
92 * Update Author information in the Posts. This function
93 * is trigger when a new Post is created or the Author
94 * has updated his/her profile.
96 * @param MongoID $id Author ID
100 function updateAuthorInfo(MongoID
$id)
102 $author = new AuthorModel
;
107 'author_name' => $author->name
,
108 'author_username' => $author->username
,
116 $this->_getCollection()->update($filter, $document, array('multiple' => true));
122 * A new post must have its author information
123 * on it. (to avoid multiple requests at render time).
129 $this->updateAuthorInfo($this->author
);
133 * Simple abstraction to add a new comment,
134 * to avoid typo errors at coding time.
136 function add_comment($user, $email, $comment)
138 $this->comment
[] = array(
141 "comment" => $comment,
148 $collection = & $this->_getCollection();
149 $collection->ensureIndex(array('uri' => 1), array('unique'=> 1, 'background' => 1));
150 $collection->ensureIndex(array('author' => 1), array('background' => 1));
151 $collection->ensureIndex(array('ts' => -1), array('background' => 1));