3 +---------------------------------------------------------------------------------+
4 | Copyright (c) 2010 ActiveMongo |
5 +---------------------------------------------------------------------------------+
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met: |
8 | 1. Redistributions of source code must retain the above copyright |
9 | notice, this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
15 | 3. All advertising materials mentioning features or use of this software |
16 | must display the following acknowledgement: |
17 | This product includes software developed by César D. Rodas. |
19 | 4. Neither the name of the César D. Rodas nor the |
20 | names of its contributors may be used to endorse or promote products |
21 | derived from this software without specific prior written permission. |
23 | THIS SOFTWARE IS PROVIDED BY CÉSAR D. RODAS ''AS IS'' AND ANY |
24 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
26 | DISCLAIMED. IN NO EVENT SHALL CÉSAR D. RODAS BE LIABLE FOR ANY |
27 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
28 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
29 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
30 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE |
33 +---------------------------------------------------------------------------------+
34 | Authors: César Rodas <crodas@php.net> |
35 +---------------------------------------------------------------------------------+
39 class PostModel
extends ActiveMongo
41 static $validates_presence_of = array(
47 static $validates_length_of = array(
48 array('title', 'min' => 5, 'max' => 30),
57 const LIMIT_PER_PAGE
=20;
60 * Return this collection name.
64 function getCollectionName()
70 * Display the blog posts entries in the correct
71 * order, getting only the needed columns, and
74 * @return PostModel This document
76 function listing_page()
79 $collection = $this->_getCollection();
81 /* Deal with MongoDB directly */
86 'author_username' => 1,
89 $cursor = $collection->find(array(), $columns);
90 $cursor->sort(array("ts" => -1))->limit(self
::LIMIT_PER_PAGE
);
93 if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0) {
94 $skip = (int)$_GET['page'] * self
::LIMIT_PER_PAGE
;
98 /* Pass our results to ActiveMongo */
99 $this->setCursor($cursor);
104 function before_validate(&$obj)
106 $obj['ts'] = new MongoTimestamp();
113 * Check if we save the Author _id,
114 * or it throws an exception.
116 * @param mixed &$value Current value
117 * @param mixed $old_value Past value, used on Update
121 function author_filter(&$value, $old_value)
123 if (!$value instanceof MongoID
) {
124 throw new Exception("Invalid MongoID");
130 * Update Author information in the Posts. This function
131 * is trigger when a new Post is created or the Author
132 * has updated his/her profile.
134 * @param MongoID $id Author ID
138 function updateAuthorInfo(MongoID
$id)
140 $author = new AuthorModel
;
145 'author_name' => $author->name
,
146 'author_username' => $author->username
,
154 $this->_getCollection()->update($filter, $document, array('multiple' => true));
160 * A new post must have its author information
161 * on it. (to avoid multiple requests at render time).
165 function after_create()
167 $this->updateAuthorInfo($this->author
);
171 * Simple abstraction to add a new comment,
172 * to avoid typo errors at coding time.
174 function add_comment($user, $email, $comment)
176 $this->comment
[] = array(
179 "comment" => $comment,
186 $this->addIndex(array('uri' => 1), array('unique'=> 1));
187 $this->addIndex(array('author' => 1));
188 $this->addIndex(array('ts' => -1));