- Changed sample directory name
[activemongo.git] / samples / blog / Post.php
blob23aa935fb46fd509933427a13837731f9cdd2ab1
1 <?php
3 class PostModel extends ActiveMongo
5 public $title;
6 public $uri;
7 public $author;
8 public $comment;
9 public $ts;
11 const LIMIT_PER_PAGE=20;
13 /**
14 * Return this collection name.
16 * @return string
18 function getCollectionName()
20 return 'post';
23 /**
24 * Display the blog posts entries in the correct
25 * order, getting only the needed columns, and
26 * adding pagination.
28 * @return PostModel This document
30 function listing_page()
32 /* Get collection */
33 $collection = $this->_getCollection();
35 /* Deal with MongoDB directly */
36 $columns = array(
37 'title' => 1,
38 'uri' => 1,
39 'author_name' => 1,
40 'author_username' => 1,
41 'ts' => 1,
43 $cursor = $collection->find(array(), $columns);
44 $cursor->sort(array("ts" => -1))->limit(self::LIMIT_PER_PAGE);
46 /* Pagination */
47 if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0) {
48 $skip = (int)$_GET['page'] * self::LIMIT_PER_PAGE;
49 $cursor->skip($skip);
52 /* Pass our results to ActiveMongo */
53 $this->setCursor($cursor);
55 return $this;
58 function pre_save($op, &$obj)
60 if ($op == "create") {
61 $obj['ts'] = new MongoTimestamp();
66 /**
67 * Author Filter.
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
75 * @return bool
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");
88 return true;
91 /**
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
98 * @return void
100 function updateAuthorInfo(MongoID $id)
102 $author = new AuthorModel;
103 $author->find($id);
105 $document = array(
106 '$set' => array(
107 'author_name' => $author->name,
108 'author_username' => $author->username,
112 $filter = array(
113 'author' => $id,
116 $this->_getCollection()->update($filter, $document, array('multiple' => true));
118 return true;
122 * A new post must have its author information
123 * on it. (to avoid multiple requests at render time).
125 * @return void
127 function on_save()
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(
139 "user" => $user,
140 "email" => $email,
141 "comment" => $comment,
143 return true;
146 function setup()
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));