test-active-relations.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. include_once('../adodb.inc.php');
  3. include_once('../adodb-active-record.inc.php');
  4. $db = NewADOConnection('mysql://root@localhost/northwind');
  5. $db->debug=1;
  6. ADOdb_Active_Record::SetDatabaseAdapter($db);
  7. $db->Execute("CREATE TEMPORARY TABLE `persons` (
  8. `id` int(10) unsigned NOT NULL auto_increment,
  9. `name_first` varchar(100) NOT NULL default '',
  10. `name_last` varchar(100) NOT NULL default '',
  11. `favorite_color` varchar(100) NOT NULL default '',
  12. PRIMARY KEY (`id`)
  13. ) ENGINE=MyISAM;
  14. ");
  15. $db->Execute("CREATE TEMPORARY TABLE `children` (
  16. `id` int(10) unsigned NOT NULL auto_increment,
  17. `person_id` int(10) unsigned NOT NULL,
  18. `name_first` varchar(100) NOT NULL default '',
  19. `name_last` varchar(100) NOT NULL default '',
  20. `favorite_pet` varchar(100) NOT NULL default '',
  21. PRIMARY KEY (`id`)
  22. ) ENGINE=MyISAM;
  23. ");
  24. $db->Execute("insert into children (person_id,name_first,name_last) values (1,'Jill','Lim')");
  25. $db->Execute("insert into children (person_id,name_first,name_last) values (1,'Joan','Lim')");
  26. $db->Execute("insert into children (person_id,name_first,name_last) values (1,'JAMIE','Lim')");
  27. ADODB_Active_Record::TableHasMany('persons', 'children','person_id');
  28. class person extends ADOdb_Active_Record{}
  29. $person = new person();
  30. # $person->HasMany('children','person_id'); ## this is affects all other instances of Person
  31. $person->name_first = 'John';
  32. $person->name_last = 'Lim';
  33. $person->favorite_color = 'lavender';
  34. $person->save(); // this save will perform an INSERT successfully
  35. $person2 = new person();
  36. $person2->Load('id=1');
  37. $c = $person2->children;
  38. if (is_array($c) && sizeof($c) == 3 && $c[0]->name_first=='Jill' && $c[1]->name_first=='Joan'
  39. && $c[2]->name_first == 'JAMIE') echo "OK Loaded HasMany</br>";
  40. else {
  41. var_dump($c);
  42. echo "error loading hasMany should have 3 array elements Jill Joan Jamie<br>";
  43. }
  44. class child extends ADOdb_Active_Record{};
  45. ADODB_Active_Record::TableBelongsTo('children','person','person_id','id');
  46. $ch = new Child('children',array('id'));
  47. $ch->Load('id=1');
  48. if ($ch->name_first !== 'Jill') echo "error in Loading Child<br>";
  49. $p = $ch->person;
  50. if (!$p || $p->name_first != 'John') echo "Error loading belongsTo<br>";
  51. else echo "OK loading BelongTo<br>";
  52. if ($p) {
  53. #$p->HasMany('children','person_id'); ## this is affects all other instances of Person
  54. $p->LoadRelations('children', 'order by id',1,2);
  55. if (sizeof($p->children) == 2 && $p->children[1]->name_first == 'JAMIE') echo "OK LoadRelations<br>";
  56. else {
  57. var_dump($p->children);
  58. echo "error LoadRelations<br>";
  59. }
  60. unset($p->children);
  61. $p->LoadRelations('children', " name_first like 'J%' order by id",1,2);
  62. }
  63. if ($p)
  64. foreach($p->children as $c) {
  65. echo " Saving $c->name_first <br>";
  66. $c->name_first .= ' K.';
  67. $c->Save();
  68. }