- Added Validators
[activemongo.git] / samples / blog / App.php
blob64dd883723da107df70a2d72348d7097487d2ec0
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 +---------------------------------------------------------------------------------+
38 require "../../lib/ActiveMongo.php";
39 require "Post.php";
40 require "Author.php";
42 ActiveMongo::connect("activemongo_blog");
44 /* delete collections */
45 PostModel::drop();
46 AuthorModel::drop();
48 /* This should be done just once */
49 ActiveMongo::install();
51 /* Create a new author
52 * The property country is not defined
53 * as an AuthorModel property, but it will
54 * be saved.
56 $author = new AuthorModel;
57 $author->username = "crodas";
58 $author->name = "Cesar Rodas";
59 $author->country = "PY";
60 $author->save();
62 /* Add one blog post */
63 $post = new PostModel;
64 $post->uri = "/hello-world";
65 $post->title = "Hello World";
66 $post->author = $author->getID();
67 /* add one comment */
68 $post->add_comment("testing", "root@foo.com", "testing comment");
69 $post->save();
71 /* add another comment */
72 $post->add_comment("testing", "root@foo.com", "cool post");
73 $post->save();
75 for ($i=0; $i < 1000; $i++) {
76 /* Add another post */
77 $post->reset(); /* reet the post object */
78 $post->uri = "/".uniqid();
79 $post->title = "Yet another post ($i)";
80 $post->author = $author->getID();
81 $post->save();
84 /* Clean up the current the resultset */
85 /* same as $post = null; $post = new Post Model */
86 /* but more efficient */
87 $post->reset();
88 $post->author = $author->getID();
89 foreach ($post->find() as $bp) {
90 var_dump("Author: ".$bp->author_name);
93 $author->name = "cesar d. rodas";
94 $author->save();
96 var_dump("Author profile has been updated");
98 /**
99 * List our blog posts in the correct order
100 * (descending by Timestamp).
102 foreach ($post->listing_page() as $bp) {
103 var_dump(array("Author" => $bp->author_name, "Title"=>$bp->title));