1 | <?php |
---|
2 | global $err_count; |
---|
3 | $err_count = 0; |
---|
4 | |
---|
5 | function found($obj, $cond) |
---|
6 | { |
---|
7 | $res = var_export($obj, true); |
---|
8 | return (strpos($res, $cond)); |
---|
9 | } |
---|
10 | |
---|
11 | function notfound($obj, $cond) |
---|
12 | { |
---|
13 | return !found($obj, $cond); |
---|
14 | } |
---|
15 | |
---|
16 | function ar_assert($bool) |
---|
17 | { |
---|
18 | global $err_count; |
---|
19 | if(!$bool) |
---|
20 | $err_count ++; |
---|
21 | return $bool; |
---|
22 | } |
---|
23 | |
---|
24 | define('WEB', true); |
---|
25 | function ar_echo($txt) |
---|
26 | { |
---|
27 | if(WEB) |
---|
28 | $txt = str_replace("\n", "<br />\n", $txt); |
---|
29 | echo $txt; |
---|
30 | } |
---|
31 | |
---|
32 | include_once('../adodb.inc.php'); |
---|
33 | include_once('../adodb-active-recordx.inc.php'); |
---|
34 | |
---|
35 | |
---|
36 | $db = NewADOConnection('mysql://root@localhost/test'); |
---|
37 | $db->debug=0; |
---|
38 | ADOdb_Active_Record::SetDatabaseAdapter($db); |
---|
39 | |
---|
40 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
41 | ar_echo("Preparing database using SQL queries (creating 'people', 'children')\n"); |
---|
42 | |
---|
43 | $db->Execute("DROP TABLE `people`"); |
---|
44 | $db->Execute("DROP TABLE `children`"); |
---|
45 | $db->Execute("DROP TABLE `artists`"); |
---|
46 | $db->Execute("DROP TABLE `songs`"); |
---|
47 | |
---|
48 | $db->Execute("CREATE TABLE `people` ( |
---|
49 | `id` int(10) unsigned NOT NULL auto_increment, |
---|
50 | `name_first` varchar(100) NOT NULL default '', |
---|
51 | `name_last` varchar(100) NOT NULL default '', |
---|
52 | `favorite_color` varchar(100) NOT NULL default '', |
---|
53 | PRIMARY KEY (`id`) |
---|
54 | ) ENGINE=MyISAM; |
---|
55 | "); |
---|
56 | $db->Execute("CREATE TABLE `children` ( |
---|
57 | `person_id` int(10) unsigned NOT NULL, |
---|
58 | `name_first` varchar(100) NOT NULL default '', |
---|
59 | `name_last` varchar(100) NOT NULL default '', |
---|
60 | `favorite_pet` varchar(100) NOT NULL default '', |
---|
61 | `id` int(10) unsigned NOT NULL auto_increment, |
---|
62 | PRIMARY KEY (`id`) |
---|
63 | ) ENGINE=MyISAM; |
---|
64 | "); |
---|
65 | |
---|
66 | $db->Execute("CREATE TABLE `artists` ( |
---|
67 | `name` varchar(100) NOT NULL default '', |
---|
68 | `artistuniqueid` int(10) unsigned NOT NULL auto_increment, |
---|
69 | PRIMARY KEY (`artistuniqueid`) |
---|
70 | ) ENGINE=MyISAM; |
---|
71 | "); |
---|
72 | |
---|
73 | $db->Execute("CREATE TABLE `songs` ( |
---|
74 | `name` varchar(100) NOT NULL default '', |
---|
75 | `artistid` int(10) NOT NULL, |
---|
76 | `recordid` int(10) unsigned NOT NULL auto_increment, |
---|
77 | PRIMARY KEY (`recordid`) |
---|
78 | ) ENGINE=MyISAM; |
---|
79 | "); |
---|
80 | |
---|
81 | $db->Execute("insert into children (person_id,name_first,name_last,favorite_pet) values (1,'Jill','Lim','tortoise')"); |
---|
82 | $db->Execute("insert into children (person_id,name_first,name_last) values (1,'Joan','Lim')"); |
---|
83 | $db->Execute("insert into children (person_id,name_first,name_last) values (1,'JAMIE','Lim')"); |
---|
84 | |
---|
85 | $db->Execute("insert into artists (artistuniqueid, name) values(1,'Elvis Costello')"); |
---|
86 | $db->Execute("insert into songs (recordid, name, artistid) values(1,'No Hiding Place', 1)"); |
---|
87 | $db->Execute("insert into songs (recordid, name, artistid) values(2,'American Gangster Time', 1)"); |
---|
88 | |
---|
89 | // This class _implicitely_ relies on the 'people' table (pluralized form of 'person') |
---|
90 | class Person extends ADOdb_Active_Record |
---|
91 | { |
---|
92 | function __construct() |
---|
93 | { |
---|
94 | parent::__construct(); |
---|
95 | $this->hasMany('children'); |
---|
96 | } |
---|
97 | } |
---|
98 | // This class _implicitely_ relies on the 'children' table |
---|
99 | class Child extends ADOdb_Active_Record |
---|
100 | { |
---|
101 | function __construct() |
---|
102 | { |
---|
103 | parent::__construct(); |
---|
104 | $this->belongsTo('person'); |
---|
105 | } |
---|
106 | } |
---|
107 | // This class _explicitely_ relies on the 'children' table and shares its metadata with Child |
---|
108 | class Kid extends ADOdb_Active_Record |
---|
109 | { |
---|
110 | function __construct() |
---|
111 | { |
---|
112 | parent::__construct('children'); |
---|
113 | $this->belongsTo('person'); |
---|
114 | } |
---|
115 | } |
---|
116 | // This class _explicitely_ relies on the 'children' table but does not share its metadata |
---|
117 | class Rugrat extends ADOdb_Active_Record |
---|
118 | { |
---|
119 | function __construct() |
---|
120 | { |
---|
121 | parent::__construct('children', false, false, array('new' => true)); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | class Artist extends ADOdb_Active_Record |
---|
126 | { |
---|
127 | function __construct() |
---|
128 | { |
---|
129 | parent::__construct('artists', array('artistuniqueid')); |
---|
130 | $this->hasMany('songs', 'artistid'); |
---|
131 | } |
---|
132 | } |
---|
133 | class Song extends ADOdb_Active_Record |
---|
134 | { |
---|
135 | function __construct() |
---|
136 | { |
---|
137 | parent::__construct('songs', array('recordid')); |
---|
138 | $this->belongsTo('artist', 'artistid'); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | ar_echo("Inserting person in 'people' table ('John Lim, he likes lavender')\n"); |
---|
143 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
144 | $person = new Person(); |
---|
145 | $person->name_first = 'John'; |
---|
146 | $person->name_last = 'Lim'; |
---|
147 | $person->favorite_color = 'lavender'; |
---|
148 | $person->save(); // this save will perform an INSERT successfully |
---|
149 | |
---|
150 | $person = new Person(); |
---|
151 | $person->name_first = 'Lady'; |
---|
152 | $person->name_last = 'Cat'; |
---|
153 | $person->favorite_color = 'green'; |
---|
154 | $person->save(); |
---|
155 | |
---|
156 | $child = new Child(); |
---|
157 | $child->name_first = 'Fluffy'; |
---|
158 | $child->name_last = 'Cat'; |
---|
159 | $child->favorite_pet = 'Cat Lady'; |
---|
160 | $child->person_id = $person->id; |
---|
161 | $child->save(); |
---|
162 | |
---|
163 | $child = new Child(); |
---|
164 | $child->name_first = 'Sun'; |
---|
165 | $child->name_last = 'Cat'; |
---|
166 | $child->favorite_pet = 'Cat Lady'; |
---|
167 | $child->person_id = $person->id; |
---|
168 | $child->save(); |
---|
169 | |
---|
170 | $err_count = 0; |
---|
171 | |
---|
172 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
173 | ar_echo("person->Find('id=1') [Lazy Method]\n"); |
---|
174 | ar_echo("person is loaded but its children will be loaded on-demand later on\n"); |
---|
175 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
176 | $person = new Person(); |
---|
177 | $people = $person->Find('id=1'); |
---|
178 | ar_echo((ar_assert(found($people, "'name_first' => 'John'"))) ? "[OK] Found John\n" : "[!!] Find failed\n"); |
---|
179 | ar_echo((ar_assert(notfound($people, "'favorite_pet' => 'tortoise'"))) ? "[OK] No relation yet\n" : "[!!] Found relation when I shouldn't\n"); |
---|
180 | ar_echo("\n-- Lazily Loading Children:\n\n"); |
---|
181 | foreach($people as $aperson) |
---|
182 | { |
---|
183 | foreach($aperson->children as $achild) |
---|
184 | { |
---|
185 | if($achild->name_first); |
---|
186 | } |
---|
187 | } |
---|
188 | ar_echo((ar_assert(found($people, "'favorite_pet' => 'tortoise'"))) ? "[OK] Found relation: child\n" : "[!!] Missing relation: child\n"); |
---|
189 | ar_echo((ar_assert(found($people, "'name_first' => 'Joan'"))) ? "[OK] Found Joan\n" : "[!!] Find failed\n"); |
---|
190 | ar_echo((ar_assert(found($people, "'name_first' => 'JAMIE'"))) ? "[OK] Found JAMIE\n" : "[!!] Find failed\n"); |
---|
191 | |
---|
192 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
193 | ar_echo("person->Find('id=1' ... ADODB_WORK_AR) [Worker Method]\n"); |
---|
194 | ar_echo("person is loaded, and so are its children\n"); |
---|
195 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
196 | $person = new Person(); |
---|
197 | $people = $person->Find('id=1', false, false, array('loading' => ADODB_WORK_AR)); |
---|
198 | ar_echo((ar_assert(found($people, "'name_first' => 'John'"))) ? "[OK] Found John\n" : "[!!] Find failed\n"); |
---|
199 | ar_echo((ar_assert(found($people, "'favorite_pet' => 'tortoise'"))) ? "[OK] Found relation: child\n" : "[!!] Missing relation: child\n"); |
---|
200 | ar_echo((ar_assert(found($people, "'name_first' => 'Joan'"))) ? "[OK] Found Joan\n" : "[!!] Find failed\n"); |
---|
201 | ar_echo((ar_assert(found($people, "'name_first' => 'JAMIE'"))) ? "[OK] Found JAMIE\n" : "[!!] Find failed\n"); |
---|
202 | |
---|
203 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
204 | ar_echo("person->Find('id=1' ... ADODB_JOIN_AR) [Join Method]\n"); |
---|
205 | ar_echo("person and its children are loaded using a single query\n"); |
---|
206 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
207 | $person = new Person(); |
---|
208 | // When I specifically ask for a join, I have to specify which table id I am looking up |
---|
209 | // otherwise the SQL parser will wonder which table's id that would be. |
---|
210 | $people = $person->Find('people.id=1', false, false, array('loading' => ADODB_JOIN_AR)); |
---|
211 | ar_echo((ar_assert(found($people, "'name_first' => 'John'"))) ? "[OK] Found John\n" : "[!!] Find failed\n"); |
---|
212 | ar_echo((ar_assert(found($people, "'favorite_pet' => 'tortoise'"))) ? "[OK] Found relation: child\n" : "[!!] Missing relation: child\n"); |
---|
213 | ar_echo((ar_assert(found($people, "'name_first' => 'Joan'"))) ? "[OK] Found Joan\n" : "[!!] Find failed\n"); |
---|
214 | ar_echo((ar_assert(found($people, "'name_first' => 'JAMIE'"))) ? "[OK] Found JAMIE\n" : "[!!] Find failed\n"); |
---|
215 | |
---|
216 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
217 | ar_echo("person->Load('people.id=1') [Join Method]\n"); |
---|
218 | ar_echo("Load() always uses the join method since it returns only one row\n"); |
---|
219 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
220 | $person = new Person(); |
---|
221 | // Under the hood, Load(), since it returns only one row, always perform a join |
---|
222 | // Therefore we need to clarify which id we are talking about. |
---|
223 | $person->Load('people.id=1'); |
---|
224 | ar_echo((ar_assert(found($person, "'name_first' => 'John'"))) ? "[OK] Found John\n" : "[!!] Find failed\n"); |
---|
225 | ar_echo((ar_assert(found($person, "'favorite_pet' => 'tortoise'"))) ? "[OK] Found relation: child\n" : "[!!] Missing relation: child\n"); |
---|
226 | ar_echo((ar_assert(found($person, "'name_first' => 'Joan'"))) ? "[OK] Found Joan\n" : "[!!] Find failed\n"); |
---|
227 | ar_echo((ar_assert(found($person, "'name_first' => 'JAMIE'"))) ? "[OK] Found JAMIE\n" : "[!!] Find failed\n"); |
---|
228 | |
---|
229 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
230 | ar_echo("child->Load('children.id=1') [Join Method]\n"); |
---|
231 | ar_echo("We are now loading from the 'children' table, not from 'people'\n"); |
---|
232 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
233 | $child = new Child(); |
---|
234 | $child->Load('children.id=1'); |
---|
235 | ar_echo((ar_assert(found($child, "'name_first' => 'Jill'"))) ? "[OK] Found Jill\n" : "[!!] Find failed\n"); |
---|
236 | ar_echo((ar_assert(found($child, "'favorite_color' => 'lavender'"))) ? "[OK] Found relation: person\n" : "[!!] Missing relation: person\n"); |
---|
237 | |
---|
238 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
239 | ar_echo("child->Find('children.id=1' ... ADODB_WORK_AR) [Worker Method]\n"); |
---|
240 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
241 | $child = new Child(); |
---|
242 | $children = $child->Find('id=1', false, false, array('loading' => ADODB_WORK_AR)); |
---|
243 | ar_echo((ar_assert(found($children, "'name_first' => 'Jill'"))) ? "[OK] Found Jill\n" : "[!!] Find failed\n"); |
---|
244 | ar_echo((ar_assert(found($children, "'favorite_color' => 'lavender'"))) ? "[OK] Found relation: person\n" : "[!!] Missing relation: person\n"); |
---|
245 | ar_echo((ar_assert(notfound($children, "'name_first' => 'Joan'"))) ? "[OK] No Joan relation\n" : "[!!] Find failed\n"); |
---|
246 | ar_echo((ar_assert(notfound($children, "'name_first' => 'JAMIE'"))) ? "[OK] No JAMIE relation\n" : "[!!] Find failed\n"); |
---|
247 | |
---|
248 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
249 | ar_echo("kid->Find('children.id=1' ... ADODB_WORK_AR) [Worker Method]\n"); |
---|
250 | ar_echo("Where we see that kid shares relationships with child because they are stored\n"); |
---|
251 | ar_echo("in the common table's metadata structure.\n"); |
---|
252 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
253 | $kid = new Kid('children'); |
---|
254 | $kids = $kid->Find('children.id=1', false, false, array('loading' => ADODB_WORK_AR)); |
---|
255 | ar_echo((ar_assert(found($kids, "'name_first' => 'Jill'"))) ? "[OK] Found Jill\n" : "[!!] Find failed\n"); |
---|
256 | ar_echo((ar_assert(found($kids, "'favorite_color' => 'lavender'"))) ? "[OK] Found relation: person\n" : "[!!] Missing relation: person\n"); |
---|
257 | ar_echo((ar_assert(notfound($kids, "'name_first' => 'Joan'"))) ? "[OK] No Joan relation\n" : "[!!] Find failed\n"); |
---|
258 | ar_echo((ar_assert(notfound($kids, "'name_first' => 'JAMIE'"))) ? "[OK] No JAMIE relation\n" : "[!!] Find failed\n"); |
---|
259 | |
---|
260 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
261 | ar_echo("kid->Find('children.id=1' ... ADODB_LAZY_AR) [Lazy Method]\n"); |
---|
262 | ar_echo("Of course, lazy loading also retrieve medata information...\n"); |
---|
263 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
264 | $kid = new Kid('children'); |
---|
265 | $kids = $kid->Find('children.id=1', false, false, array('loading' => ADODB_LAZY_AR)); |
---|
266 | ar_echo((ar_assert(found($kids, "'name_first' => 'Jill'"))) ? "[OK] Found Jill\n" : "[!!] Find failed\n"); |
---|
267 | ar_echo((ar_assert(notfound($kids, "'favorite_color' => 'lavender'"))) ? "[OK] No relation yet\n" : "[!!] Found relation when I shouldn't\n"); |
---|
268 | ar_echo("\n-- Lazily Loading People:\n\n"); |
---|
269 | foreach($kids as $akid) |
---|
270 | { |
---|
271 | if($akid->person); |
---|
272 | } |
---|
273 | ar_echo((ar_assert(found($kids, "'favorite_color' => 'lavender'"))) ? "[OK] Found relation: person\n" : "[!!] Missing relation: person\n"); |
---|
274 | ar_echo((ar_assert(notfound($kids, "'name_first' => 'Joan'"))) ? "[OK] No Joan relation\n" : "[!!] Found relation when I shouldn't\n"); |
---|
275 | ar_echo((ar_assert(notfound($kids, "'name_first' => 'JAMIE'"))) ? "[OK] No JAMIE relation\n" : "[!!] Found relation when I shouldn't\n"); |
---|
276 | |
---|
277 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
278 | ar_echo("rugrat->Find('children.id=1' ... ADODB_WORK_AR) [Worker Method]\n"); |
---|
279 | ar_echo("In rugrat's constructor it is specified that\nit must forget any existing relation\n"); |
---|
280 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
281 | $rugrat = new Rugrat('children'); |
---|
282 | $rugrats = $rugrat->Find('children.id=1', false, false, array('loading' => ADODB_WORK_AR)); |
---|
283 | ar_echo((ar_assert(found($rugrats, "'name_first' => 'Jill'"))) ? "[OK] Found Jill\n" : "[!!] Find failed\n"); |
---|
284 | ar_echo((ar_assert(notfound($rugrats, "'favorite_color' => 'lavender'"))) ? "[OK] No relation found\n" : "[!!] Found relation when I shouldn't\n"); |
---|
285 | ar_echo((ar_assert(notfound($rugrats, "'name_first' => 'Joan'"))) ? "[OK] No Joan relation\n" : "[!!] Found relation when I shouldn't\n"); |
---|
286 | ar_echo((ar_assert(notfound($rugrats, "'name_first' => 'JAMIE'"))) ? "[OK] No JAMIE relation\n" : "[!!] Found relation when I shouldn't\n"); |
---|
287 | |
---|
288 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
289 | ar_echo("kid->Find('children.id=1' ... ADODB_WORK_AR) [Worker Method]\n"); |
---|
290 | ar_echo("Note how only rugrat forgot its relations - kid is fine.\n"); |
---|
291 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
292 | $kid = new Kid('children'); |
---|
293 | $kids = $kid->Find('children.id=1', false, false, array('loading' => ADODB_WORK_AR)); |
---|
294 | ar_echo((ar_assert(found($kids, "'name_first' => 'Jill'"))) ? "[OK] Found Jill\n" : "[!!] Find failed\n"); |
---|
295 | ar_echo((ar_assert(found($kids, "'favorite_color' => 'lavender'"))) ? "[OK] I did not forget relation: person\n" : "[!!] I should not have forgotten relation: person\n"); |
---|
296 | ar_echo((ar_assert(notfound($kids, "'name_first' => 'Joan'"))) ? "[OK] No Joan relation\n" : "[!!] Found relation when I shouldn't\n"); |
---|
297 | ar_echo((ar_assert(notfound($kids, "'name_first' => 'JAMIE'"))) ? "[OK] No JAMIE relation\n" : "[!!] Found relation when I shouldn't\n"); |
---|
298 | |
---|
299 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
300 | ar_echo("rugrat->Find('children.id=1' ... ADODB_WORK_AR) [Worker Method]\n"); |
---|
301 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
302 | $rugrat = new Rugrat('children'); |
---|
303 | $rugrats = $rugrat->Find('children.id=1', false, false, array('loading' => ADODB_WORK_AR)); |
---|
304 | $arugrat = $rugrats[0]; |
---|
305 | ar_echo((ar_assert(found($arugrat, "'name_first' => 'Jill'"))) ? "[OK] Found Jill\n" : "[!!] Find failed\n"); |
---|
306 | ar_echo((ar_assert(notfound($arugrat, "'favorite_color' => 'lavender'"))) ? "[OK] No relation yet\n" : "[!!] Found relation when I shouldn't\n"); |
---|
307 | |
---|
308 | ar_echo("\n-- Loading relations:\n\n"); |
---|
309 | $arugrat->belongsTo('person'); |
---|
310 | $arugrat->LoadRelations('person', 'order by id', 0, 2); |
---|
311 | ar_echo((ar_assert(found($arugrat, "'favorite_color' => 'lavender'"))) ? "[OK] Found relation: person\n" : "[!!] Missing relation: person\n"); |
---|
312 | ar_echo((ar_assert(found($arugrat, "'name_first' => 'Jill'"))) ? "[OK] Found Jill\n" : "[!!] Find failed\n"); |
---|
313 | ar_echo((ar_assert(notfound($arugrat, "'name_first' => 'Joan'"))) ? "[OK] No Joan relation\n" : "[!!] Found relation when I shouldn't\n"); |
---|
314 | ar_echo((ar_assert(notfound($arugrat, "'name_first' => 'JAMIE'"))) ? "[OK] No Joan relation\n" : "[!!] Found relation when I shouldn't\n"); |
---|
315 | |
---|
316 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
317 | ar_echo("person->Find('1=1') [Lazy Method]\n"); |
---|
318 | ar_echo("And now for our finale...\n"); |
---|
319 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
320 | $person = new Person(); |
---|
321 | $people = $person->Find('1=1', false, false, array('loading' => ADODB_LAZY_AR)); |
---|
322 | ar_echo((ar_assert(found($people, "'name_first' => 'John'"))) ? "[OK] Found John\n" : "[!!] Find failed\n"); |
---|
323 | ar_echo((ar_assert(notfound($people, "'favorite_pet' => 'tortoise'"))) ? "[OK] No relation yet\n" : "[!!] Found relation when I shouldn't\n"); |
---|
324 | ar_echo((ar_assert(notfound($people, "'name_first' => 'Fluffy'"))) ? "[OK] No Fluffy yet\n" : "[!!] Found Fluffy relation when I shouldn't\n"); |
---|
325 | ar_echo("\n-- Lazily Loading Everybody:\n\n"); |
---|
326 | foreach($people as $aperson) |
---|
327 | { |
---|
328 | foreach($aperson->children as $achild) |
---|
329 | { |
---|
330 | if($achild->name_first); |
---|
331 | } |
---|
332 | } |
---|
333 | ar_echo((ar_assert(found($people, "'favorite_pet' => 'tortoise'"))) ? "[OK] Found relation: child\n" : "[!!] Missing relation: child\n"); |
---|
334 | ar_echo((ar_assert(found($people, "'name_first' => 'Joan'"))) ? "[OK] Found Joan\n" : "[!!] Find failed\n"); |
---|
335 | ar_echo((ar_assert(found($people, "'name_first' => 'JAMIE'"))) ? "[OK] Found JAMIE\n" : "[!!] Find failed\n"); |
---|
336 | ar_echo((ar_assert(found($people, "'name_first' => 'Lady'"))) ? "[OK] Found Cat Lady\n" : "[!!] Find failed\n"); |
---|
337 | ar_echo((ar_assert(found($people, "'name_first' => 'Fluffy'"))) ? "[OK] Found Fluffy\n" : "[!!] Find failed\n"); |
---|
338 | ar_echo((ar_assert(found($people, "'name_first' => 'Sun'"))) ? "[OK] Found Sun\n" : "[!!] Find failed\n"); |
---|
339 | |
---|
340 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
341 | ar_echo("artist->Load('artistuniqueid=1') [Join Method]\n"); |
---|
342 | ar_echo("Yes, we are dabbling in the musical field now..\n"); |
---|
343 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
344 | $artist = new Artist(); |
---|
345 | $artist->Load('artistuniqueid=1'); |
---|
346 | ar_echo((ar_assert(found($artist, "'name' => 'Elvis Costello'"))) ? "[OK] Found Elvis Costello\n" : "[!!] Find failed\n"); |
---|
347 | ar_echo((ar_assert(found($artist, "'name' => 'No Hiding Place'"))) ? "[OK] Found relation: song\n" : "[!!] Missing relation: song\n"); |
---|
348 | |
---|
349 | |
---|
350 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
351 | ar_echo("song->Load('recordid=1') [Join Method]\n"); |
---|
352 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
353 | $song = new Song(); |
---|
354 | $song->Load('recordid=1'); |
---|
355 | ar_echo((ar_assert(found($song, "'name' => 'No Hiding Place'"))) ? "[OK] Found song\n" : "[!!] Find failed\n"); |
---|
356 | |
---|
357 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
358 | ar_echo("artist->Find('artistuniqueid=1' ... ADODB_JOIN_AR) [Join Method]\n"); |
---|
359 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
360 | $artist = new Artist(); |
---|
361 | $artists = $artist->Find('artistuniqueid=1', false, false, array('loading' => ADODB_JOIN_AR)); |
---|
362 | ar_echo((ar_assert(found($artists, "'name' => 'Elvis Costello'"))) ? "[OK] Found Elvis Costello\n" : "[!!] Find failed\n"); |
---|
363 | ar_echo((ar_assert(found($artists, "'name' => 'No Hiding Place'"))) ? "[OK] Found relation: song\n" : "[!!] Missing relation: song\n"); |
---|
364 | |
---|
365 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
366 | ar_echo("song->Find('recordid=1' ... ADODB_JOIN_AR) [Join Method]\n"); |
---|
367 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
368 | $song = new Song(); |
---|
369 | $songs = $song->Find('recordid=1', false, false, array('loading' => ADODB_JOIN_AR)); |
---|
370 | ar_echo((ar_assert(found($songs, "'name' => 'No Hiding Place'"))) ? "[OK] Found song\n" : "[!!] Find failed\n"); |
---|
371 | |
---|
372 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
373 | ar_echo("artist->Find('artistuniqueid=1' ... ADODB_WORK_AR) [Work Method]\n"); |
---|
374 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
375 | $artist = new Artist(); |
---|
376 | $artists = $artist->Find('artistuniqueid=1', false, false, array('loading' => ADODB_WORK_AR)); |
---|
377 | ar_echo((ar_assert(found($artists, "'name' => 'Elvis Costello'"))) ? "[OK] Found Elvis Costello\n" : "[!!] Find failed\n"); |
---|
378 | ar_echo((ar_assert(found($artists, "'name' => 'No Hiding Place'"))) ? "[OK] Found relation: song\n" : "[!!] Missing relation: song\n"); |
---|
379 | |
---|
380 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
381 | ar_echo("song->Find('recordid=1' ... ADODB_JOIN_AR) [Join Method]\n"); |
---|
382 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
383 | $song = new Song(); |
---|
384 | $songs = $song->Find('recordid=1', false, false, array('loading' => ADODB_WORK_AR)); |
---|
385 | ar_echo((ar_assert(found($songs, "'name' => 'No Hiding Place'"))) ? "[OK] Found song\n" : "[!!] Find failed\n"); |
---|
386 | |
---|
387 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
388 | ar_echo("artist->Find('artistuniqueid=1' ... ADODB_LAZY_AR) [Lazy Method]\n"); |
---|
389 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
390 | $artist = new Artist(); |
---|
391 | $artists = $artist->Find('artistuniqueid=1', false, false, array('loading' => ADODB_LAZY_AR)); |
---|
392 | ar_echo((ar_assert(found($artists, "'name' => 'Elvis Costello'"))) ? "[OK] Found Elvis Costello\n" : "[!!] Find failed\n"); |
---|
393 | ar_echo((ar_assert(notfound($artists, "'name' => 'No Hiding Place'"))) ? "[OK] No relation yet\n" : "[!!] Found relation when I shouldn't\n"); |
---|
394 | foreach($artists as $anartist) |
---|
395 | { |
---|
396 | foreach($anartist->songs as $asong) |
---|
397 | { |
---|
398 | if($asong->name); |
---|
399 | } |
---|
400 | } |
---|
401 | ar_echo((ar_assert(found($artists, "'name' => 'No Hiding Place'"))) ? "[OK] Found relation: song\n" : "[!!] Missing relation: song\n"); |
---|
402 | |
---|
403 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
404 | ar_echo("song->Find('recordid=1' ... ADODB_LAZY_AR) [Lazy Method]\n"); |
---|
405 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
406 | $song = new Song(); |
---|
407 | $songs = $song->Find('recordid=1', false, false, array('loading' => ADODB_LAZY_AR)); |
---|
408 | ar_echo((ar_assert(found($songs, "'name' => 'No Hiding Place'"))) ? "[OK] Found song\n" : "[!!] Find failed\n"); |
---|
409 | ar_echo((ar_assert(notfound($songs, "'name' => 'Elvis Costello'"))) ? "[OK] No relation yet\n" : "[!!] Found relation when I shouldn't\n"); |
---|
410 | foreach($songs as $asong) |
---|
411 | { |
---|
412 | if($asong->artist); |
---|
413 | } |
---|
414 | ar_echo((ar_assert(found($songs, "'name' => 'Elvis Costello'"))) ? "[OK] Found relation: artist\n" : "[!!] Missing relation: artist\n"); |
---|
415 | |
---|
416 | ar_echo("\n\n-------------------------------------------------------------------------------------------------------------------\n"); |
---|
417 | ar_echo("Test suite complete. " . (($err_count > 0) ? "$err_count errors found.\n" : "Success.\n")); |
---|
418 | ar_echo("-------------------------------------------------------------------------------------------------------------------\n"); |
---|
419 | ?> |
---|