test-active-relationsx.php 24 KB

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