My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
Changes to /wiki/BasicUsage.wiki
r141 vs. r146 Compare: vs.  Format:
Revision r146
Go to: 
Project members, sign in to write a code review
/wiki/BasicUsage.wiki   r141 /wiki/BasicUsage.wiki   r146
1 #summary Basic usage of the Morph package
2 #labels Featured,Phase-Implementation 1 #labels Featured,Phase-Implementation
3 2
4 =Basic Usage= 3 *MOVED TO [https://github.com/a-musing-moose/morph GITHUB]*
5
6 This tutorial guides you through the basic operations of Morph
7
8 ==Defining Storable object==
9
10 Defining objects that can be stored in MongoDB is quite straight forward. All you need to do is write classes that extend Morph_Object and specifying
11 the fields that it should store using the addProperty function().
12
13 For example:
14
15 {{{
16
17 class Book extends Morph_Object
18 {
19
20 public function __construct($id = null)
21 {
22 parent::__construct($id);
23 $this->addProperty(new Morph_Property_String('title'))
24 ->addProperty(new Morph_Property_String('author'))
25 ->addProperty(new Morph_Property_Integer('pageNumber'))
26 ->addProperty(new Morph_Property_Date('publishedDate'));
27 }
28
29 }
30
31 }}}
32
33 The class defined above represents a book and holds 4 storable properties:
34
35 * *title* - A string to hold the title of the book
36 * *author* - A string to hold the author of the book
37 * *pageNumber* - An integer to hold the number of pages in the book
38 * *published* - A date to hold the date that the book was published
39
40 ==Saving and Retrieving Objects==
41
42 Saving and retrieving of object is achieved through the object itself.
43
44
45 The following example code show how to create a new Book object and save it to the database
46 'MyDatabase'
47
48 {{{
49
50 $mongo = new Mongo(); //or MongoAuth etc..
51 Morph_Storage::init($mongo->selectDB('MyDatabase'));
52
53 $aBook = new Book();
54 $aBook->title = 'MongoDB Rocks!';
55 $aBook->author = 'Jonathan Moss';
56 $aBook->pageNumber = 150;
57 $aBook->publishedDate = strtotime('2009-09-12');
58
59 $aBook->save();
60
61 }}}
62
63 To retrieve the a book object by it's id you just need to do the following:
64
65 {{{
66
67 $mongo = new Mongo();
68 Morph_Storage::init($mongo->selectDB('MyDatabase'));
69
70 $aBook = new Book();
71 $aBook->loadById('MyIdNumber1');
72
73 echo $aBook; //all Morph_Object derived classes inherit a __toString() method which is useful for debugging.
74
75 }}}
76
77 ==Querying the Database==
78
79 Morph_Object derived classes also provides the method findByQuery which will find objects in MongoDB and return PHP objects. The key to this method
80 is the class Morph_Query which can be used to define a query in an object oriented fashion. e.g.:
81
82 {{{
83
84 $mongo = new Mongo();
85 Morph_Storage::init($mongo->selectDB('MyDatabase'));
86
87 $aBook = new Book();
88
89 $query = new Morph_Query();
90 $query->property('author')->equals('Jonathan Moss');
91
92 $resultsSet = $aBook->findByQuery($query);
93
94 }}}
95
96 You can also build more complex queries consisting of several constraints such as the one below which will return all Book objects
97 that have the *author* 'Jonathan Moss' and have a *pageNumber* between 50 and 1000.
98
99 {{{
100
101 $mongo = new Mongo();
102 Morph_Storage::init($mongo->selectDB('MyDatabase'));
103
104 $aBook = new Book();
105
106 $query = new Morph_Query();
107 $query->property('author')
108 ->equals('Jonathan Moss')
109 ->property('pageNumber')
110 ->lessThan(1000)
111 ->greaterThan(50);
112
113 $resultsSet = $aBook->findByQuery($query);
114
115 }}}
116
117 findByQuery() does not return all the results straight away. Instead it provides an instance of Morph_Iterator. Morph_Iterator implements the Iterator interface and can therefore be used most easily in foreach loops. For example:
118
119 {{{
120
121 foreach($resultSet as $aBook) {
122 echo $aBook->title . "\n";
123 }
124
125 }}}
Powered by Google Project Hosting