|
Adodb Dokumentation
V5.14 8 Sept 2011
|
00001 <?php 00002 00003 include_once('../adodb.inc.php'); 00004 include_once('../adodb-active-record.inc.php'); 00005 00006 00007 $db = NewADOConnection('mysql://root@localhost/northwind'); 00008 $db->debug=1; 00009 ADOdb_Active_Record::SetDatabaseAdapter($db); 00010 00011 $db->Execute("CREATE TEMPORARY TABLE `persons` ( 00012 `id` int(10) unsigned NOT NULL auto_increment, 00013 `name_first` varchar(100) NOT NULL default '', 00014 `name_last` varchar(100) NOT NULL default '', 00015 `favorite_color` varchar(100) NOT NULL default '', 00016 PRIMARY KEY (`id`) 00017 ) ENGINE=MyISAM; 00018 "); 00019 00020 $db->Execute("CREATE TEMPORARY TABLE `children` ( 00021 `id` int(10) unsigned NOT NULL auto_increment, 00022 `person_id` int(10) unsigned NOT NULL, 00023 `name_first` varchar(100) NOT NULL default '', 00024 `name_last` varchar(100) NOT NULL default '', 00025 `favorite_pet` varchar(100) NOT NULL default '', 00026 PRIMARY KEY (`id`) 00027 ) ENGINE=MyISAM; 00028 "); 00029 00030 00031 $db->Execute("insert into children (person_id,name_first,name_last) values (1,'Jill','Lim')"); 00032 $db->Execute("insert into children (person_id,name_first,name_last) values (1,'Joan','Lim')"); 00033 $db->Execute("insert into children (person_id,name_first,name_last) values (1,'JAMIE','Lim')"); 00034 00035 ADODB_Active_Record::TableHasMany('persons', 'children','person_id'); 00036 class person extends ADOdb_Active_Record{} 00037 00038 $person = new person(); 00039 # $person->HasMany('children','person_id'); ## this is affects all other instances of Person 00040 00041 $person->name_first = 'John'; 00042 $person->name_last = 'Lim'; 00043 $person->favorite_color = 'lavender'; 00044 $person->save(); // this save will perform an INSERT successfully 00045 00046 $person2 = new person(); 00047 $person2->Load('id=1'); 00048 00049 $c = $person2->children; 00050 if (is_array($c) && sizeof($c) == 3 && $c[0]->name_first=='Jill' && $c[1]->name_first=='Joan' 00051 && $c[2]->name_first == 'JAMIE') echo "OK Loaded HasMany</br>"; 00052 else { 00053 var_dump($c); 00054 echo "error loading hasMany should have 3 array elements Jill Joan Jamie<br>"; 00055 } 00056 00057 class child extends ADOdb_Active_Record{}; 00058 ADODB_Active_Record::TableBelongsTo('children','person','person_id','id'); 00059 $ch = new Child('children',array('id')); 00060 00061 $ch->Load('id=1'); 00062 if ($ch->name_first !== 'Jill') echo "error in Loading Child<br>"; 00063 00064 $p = $ch->person; 00065 if (!$p || $p->name_first != 'John') echo "Error loading belongsTo<br>"; 00066 else echo "OK loading BelongTo<br>"; 00067 00068 if ($p) { 00069 #$p->HasMany('children','person_id'); ## this is affects all other instances of Person 00070 $p->LoadRelations('children', 'order by id',1,2); 00071 if (sizeof($p->children) == 2 && $p->children[1]->name_first == 'JAMIE') echo "OK LoadRelations<br>"; 00072 else { 00073 var_dump($p->children); 00074 echo "error LoadRelations<br>"; 00075 } 00076 00077 unset($p->children); 00078 $p->LoadRelations('children', " name_first like 'J%' order by id",1,2); 00079 } 00080 if ($p) 00081 foreach($p->children as $c) { 00082 echo " Saving $c->name_first <br>"; 00083 $c->name_first .= ' K.'; 00084 $c->Save(); 00085 } 00086 00087 ?>