- Added query abstraction (doQuery, limit, where, columns)
[activemongo.git] / samples / blog / Post.php
blob50761b60e12e1002aeed1f94be438f7f492bac30
1 <?php
2 /*
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. |
10 | |
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. |
14 | |
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. |
18 | |
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. |
22 | |
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(
42 'title',
43 'uri',
44 'author',
47 static $validates_length_of = array(
48 array('title', 'min' => 5, 'max' => 30),
51 public $title;
52 public $uri;
53 public $author;
54 public $comment;
55 public $ts;
57 const LIMIT_PER_PAGE=20;
59 /**
60 * Return this collection name.
62 * @return string
64 function getCollectionName()
66 return 'post';
69 function before_validate(&$obj)
71 $obj['ts'] = new MongoTimestamp();
75 /**
76 * Author Filter.
78 * Check if we save the Author _id,
79 * or it throws an exception.
81 * @param mixed &$value Current value
82 * @param mixed $old_value Past value, used on Update
84 * @return bool
86 function author_filter(&$value, $old_value)
88 if (!$value instanceof MongoID) {
89 throw new Exception("Invalid MongoID");
91 return true;
94 /**
95 * Update Author information in the Posts. This function
96 * is trigger when a new Post is created or the Author
97 * has updated his/her profile.
99 * @param MongoID $id Author ID
101 * @return void
103 function updateAuthorInfo(MongoID $id)
105 $author = new AuthorModel;
106 $author->find($id);
108 $document = array(
109 '$set' => array(
110 'author_name' => $author->name,
111 'author_username' => $author->username,
115 $filter = array(
116 'author' => $id,
119 $this->_getCollection()->update($filter, $document, array('multiple' => true));
121 return true;
125 * A new post must have its author information
126 * on it. (to avoid multiple requests at render time).
128 * @return void
130 function after_create()
132 $this->updateAuthorInfo($this->author);
136 * Simple abstraction to add a new comment,
137 * to avoid typo errors at coding time.
139 function add_comment($user, $email, $comment)
141 $this->comment[] = array(
142 "user" => $user,
143 "email" => $email,
144 "comment" => $comment,
146 return true;
149 function setup()
151 $this->addIndex(array('uri' => 1), array('unique'=> 1));
152 $this->addIndex(array('author' => 1));
153 $this->addIndex(array('ts' => -1));