Adodb Dokumentation  V5.14 8 Sept 2011
tests/test-active-record.php
00001 <?php
00002 
00003         include_once('../adodb.inc.php');
00004         include_once('../adodb-active-record.inc.php');
00005         
00006         // uncomment the following if you want to test exceptions
00007         if (@$_GET['except']) {
00008                 if (PHP_VERSION >= 5) {
00009                         include('../adodb-exceptions.inc.php');
00010                         echo "<h3>Exceptions included</h3>";
00011                 }
00012         }
00013 
00014         $db = NewADOConnection('mysql://root@localhost/northwind?persist');
00015         $db->debug=1;
00016         ADOdb_Active_Record::SetDatabaseAdapter($db);
00017 
00018         
00019         $db->Execute("CREATE TEMPORARY TABLE `persons` (
00020                         `id` int(10) unsigned NOT NULL auto_increment,
00021                         `name_first` varchar(100) NOT NULL default '',
00022                         `name_last` varchar(100) NOT NULL default '',
00023                         `favorite_color` varchar(100) NOT NULL default '',
00024                         PRIMARY KEY  (`id`)
00025                     ) ENGINE=MyISAM;
00026                    ");
00027                            
00028         $db->Execute("CREATE TEMPORARY TABLE `children` (
00029                         `id` int(10) unsigned NOT NULL auto_increment,
00030                                         `person_id` int(10) unsigned NOT NULL,
00031                         `name_first` varchar(100) NOT NULL default '',
00032                         `name_last` varchar(100) NOT NULL default '',
00033                         `favorite_pet` varchar(100) NOT NULL default '',
00034                         PRIMARY KEY  (`id`)
00035                     ) ENGINE=MyISAM;
00036                    ");
00037                            
00038         class Person extends ADOdb_Active_Record{ function ret($v) {return $v;} }
00039         $person = new Person();
00040         ADOdb_Active_Record::$_quoteNames = '111';
00041         
00042         echo "<p>Output of getAttributeNames: ";
00043         var_dump($person->getAttributeNames());
00044         
00059         $person = new Person();
00060         $person->name_first = 'Andi';
00061         $person->name_last  = 'Gutmans';
00062         $person->save(); // this save() will fail on INSERT as favorite_color is a must fill...
00063         
00064         
00065         $person = new Person();
00066         $person->name_first     = 'Andi';
00067         $person->name_last      = 'Gutmans';
00068         $person->favorite_color = 'blue';
00069         $person->save(); // this save will perform an INSERT successfully
00070         
00071         echo "<p>The Insert ID generated:"; print_r($person->id);
00072         
00073         $person->favorite_color = 'red';
00074         $person->save(); // this save() will perform an UPDATE
00075         
00076         $person = new Person();
00077         $person->name_first     = 'John';
00078         $person->name_last      = 'Lim';
00079         $person->favorite_color = 'lavender';
00080         $person->save(); // this save will perform an INSERT successfully
00081         
00082         // load record where id=2 into a new ADOdb_Active_Record
00083         $person2 = new Person();
00084         $person2->Load('id=2');
00085         
00086         $activeArr = $db->GetActiveRecordsClass($class = "Person",$table = "Persons","id=".$db->Param(0),array(2));
00087         $person2 = $activeArr[0];
00088         echo "<p>Name (should be John): ",$person->name_first, " <br> Class (should be Person): ",get_class($person2),"<br>";   
00089         
00090         $db->Execute("insert into children (person_id,name_first,name_last) values (2,'Jill','Lim')");
00091         $db->Execute("insert into children (person_id,name_first,name_last) values (2,'Joan','Lim')");
00092         $db->Execute("insert into children (person_id,name_first,name_last) values (2,'JAMIE','Lim')");
00093         
00094         $newperson2 = new Person();
00095         $person2->HasMany('children','person_id');
00096         $person2->Load('id=2');
00097         $person2->name_last='green';
00098         $c = $person2->children;
00099         $person2->save();
00100 
00101         if (is_array($c) && sizeof($c) == 3 && $c[0]->name_first=='Jill' && $c[1]->name_first=='Joan'
00102                 && $c[2]->name_first == 'JAMIE') echo "OK Loaded HasMany</br>";
00103         else {
00104                 var_dump($c);
00105                 echo "error loading hasMany should have 3 array elements Jill Joan Jamie<br>";
00106         }
00107         
00108         class Child extends ADOdb_Active_Record{};
00109         $ch = new Child('children',array('id'));
00110         $ch->BelongsTo('person','person_id','id');
00111         $ch->Load('id=1');
00112         if ($ch->name_first !== 'Jill') echo "error in Loading Child<br>";
00113         
00114         $p = $ch->person;
00115         if ($p->name_first != 'John') echo "Error loading belongsTo<br>";
00116         else echo "OK loading BelongTo<br>";
00117 
00118         $p->hasMany('children','person_id');
00119         $p->LoadRelations('children', " Name_first like 'J%' order by id",1,2);
00120         if (sizeof($p->children) == 2 && $p->children[1]->name_first == 'JAMIE') echo "OK LoadRelations<br>";
00121         else echo "error LoadRelations<br>";
00122         
00123                 $db->Execute("CREATE TEMPORARY TABLE `persons2` (
00124                         `id` int(10) unsigned NOT NULL auto_increment,
00125                         `name_first` varchar(100) NOT NULL default '',
00126                         `name_last` varchar(100) NOT NULL default '',
00127                         `favorite_color` varchar(100) default '',
00128                         PRIMARY KEY  (`id`)
00129                     ) ENGINE=MyISAM;
00130                    ");
00131         
00132         $p = new adodb_active_record('persons2');
00133         $p->name_first = 'James';
00134         
00135         $p->name_last = 'James';
00136         
00137         $p->HasMany('children','person_id');
00138         $p->children;
00139         var_dump($p);
00140         $p->Save();
00141 ?>