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()
69 function before_validate(&$obj)
71 $obj['ts'] = new MongoTimestamp();
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
86 function author_filter(&$value, $old_value)
88 if (!$value instanceof MongoID
) {
89 throw new Exception("Invalid MongoID");
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
103 function updateAuthorInfo(MongoID
$id)
105 $author = new AuthorModel
;
106 $author->where('id', $id);
108 /* perform the query */
113 'author_name' => $author->name
,
114 'author_username' => $author->username
,
122 $this->_getCollection()->update($filter, $document, array('multiple' => TRUE));
128 * A new post must have its author information
129 * on it. (to avoid multiple requests at render time).
133 function after_create()
135 $this->updateAuthorInfo($this->author
);
139 * Simple abstraction to add a new comment,
140 * to avoid typo errors at coding time.
142 function add_comment($user, $email, $comment)
144 $this->comment
[] = array(
147 "comment" => $comment,
154 $this->addIndex(array('uri' => 1), array('unique'=> 1));
155 $this->addIndex(array('author' => 1));
156 $this->addIndex(array('ts' => -1));
162 var_dump(array('a' => 1, 'a'=>2));
163 $c = $this->_getCollection();
164 //$d = $c->find(array('author_name' => array('$all' => array(new MongoRegex("/^c/"), new MongoRegex('/s$/') ))));
165 $d = $c->find(array('author_name' => array('$all' => array(new MongoRegex("/^c/"), new MongoRegex('/s$/') ))));
167 var_dump($d->count());
168 var_dump($d->info());