Tests for new find types
[activemongo.git] / samples / dynamic_references / App.php
blob63b9002bae85f328cc1413be6100e4f83149b3e4
1 <?php
3 require "../../lib/ActiveMongo.php";
4 require "User.php";
5 require "Services.php";
7 ActiveMongo::connect("test");
9 User::drop();
10 Service::drop();
12 /* Create an user for our 'aggregator' */
13 $user = new User;
14 $user->username = "crodas";
15 $user->password = "crodas";
16 $user->save();
18 /* Now we are going to query for the services for
19 * the new user, it will return an empty
20 * dataset, but we're going to create a 'dynamic' reference
21 * to the 'query'. When this reference is deference
22 * then same 'query' is going to be asked to the database.
24 $service = new Service;
25 $service->where('user', $user->getID())->doQuery();
27 /* save reference */
28 $user->services = $service->getReference(TRUE);
29 $user->save();
32 /* Create one service, note that Service::user is $user->getID() */
33 $twt = new Twitter;
34 $twt->user = $user->getID();
35 $twt->rss = "http://twitter.com/statuses/user_timeline/crodas.rss";
36 $twt->save();
38 /* Create another service */
39 $blg = new Blog;
40 $blg->user = $user->getID();
41 $blg->rss = "http://crodas.org/feed/rss";
42 $blg->save();
44 /* Create another service */
45 $blg1 = new Blog;
46 $blg1->user = $user->getID();
47 $blg1->rss = "http://crodas.org/feed/rss";
48 $blg1->save();
50 /* Delete current objects */
51 unset($user, $blg, $blg1, $twt);
53 /* Output the document that is going to be sent to MongoDB */
54 $debug=TRUE;
56 $users = new User;
57 foreach ($users->where('username', 'crodas') as $user) {
58 /* Load all references */
59 $user->doDeferencing();
61 /* Modify the first service */
62 $user->services[0]->title = 'English Blog';
64 /* You need to save the referenced document */
65 /* explicitly */
66 $user->services[0]->save();
68 /* Modify the current user */
69 $user->foobar = 'cesar';
70 $user->Save();