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