1 | <?php |
---|
2 | |
---|
3 | include_once('../adodb.inc.php'); |
---|
4 | include_once('../adodb-active-record.inc.php'); |
---|
5 | |
---|
6 | // uncomment the following if you want to test exceptions |
---|
7 | if (@$_GET['except']) { |
---|
8 | if (PHP_VERSION >= 5) { |
---|
9 | include('../adodb-exceptions.inc.php'); |
---|
10 | echo "<h3>Exceptions included</h3>"; |
---|
11 | } |
---|
12 | } |
---|
13 | |
---|
14 | $db = NewADOConnection('mysql://root@localhost/northwind?persist'); |
---|
15 | $db->debug=1; |
---|
16 | ADOdb_Active_Record::SetDatabaseAdapter($db); |
---|
17 | |
---|
18 | |
---|
19 | $db->Execute("CREATE TEMPORARY TABLE `persons` ( |
---|
20 | `id` int(10) unsigned NOT NULL auto_increment, |
---|
21 | `name_first` varchar(100) NOT NULL default '', |
---|
22 | `name_last` varchar(100) NOT NULL default '', |
---|
23 | `favorite_color` varchar(100) NOT NULL default '', |
---|
24 | PRIMARY KEY (`id`) |
---|
25 | ) ENGINE=MyISAM; |
---|
26 | "); |
---|
27 | |
---|
28 | $db->Execute("CREATE TEMPORARY TABLE `children` ( |
---|
29 | `id` int(10) unsigned NOT NULL auto_increment, |
---|
30 | `person_id` int(10) unsigned NOT NULL, |
---|
31 | `name_first` varchar(100) NOT NULL default '', |
---|
32 | `name_last` varchar(100) NOT NULL default '', |
---|
33 | `favorite_pet` varchar(100) NOT NULL default '', |
---|
34 | PRIMARY KEY (`id`) |
---|
35 | ) ENGINE=MyISAM; |
---|
36 | "); |
---|
37 | |
---|
38 | class Person extends ADOdb_Active_Record{ function ret($v) {return $v;} } |
---|
39 | $person = new Person(); |
---|
40 | ADOdb_Active_Record::$_quoteNames = '111'; |
---|
41 | |
---|
42 | echo "<p>Output of getAttributeNames: "; |
---|
43 | var_dump($person->getAttributeNames()); |
---|
44 | |
---|
45 | /** |
---|
46 | * Outputs the following: |
---|
47 | * array(4) { |
---|
48 | * [0]=> |
---|
49 | * string(2) "id" |
---|
50 | * [1]=> |
---|
51 | * string(9) "name_first" |
---|
52 | * [2]=> |
---|
53 | * string(8) "name_last" |
---|
54 | * [3]=> |
---|
55 | * string(13) "favorite_color" |
---|
56 | * } |
---|
57 | */ |
---|
58 | |
---|
59 | $person = new Person(); |
---|
60 | $person->name_first = 'Andi'; |
---|
61 | $person->name_last = 'Gutmans'; |
---|
62 | $person->save(); // this save() will fail on INSERT as favorite_color is a must fill... |
---|
63 | |
---|
64 | |
---|
65 | $person = new Person(); |
---|
66 | $person->name_first = 'Andi'; |
---|
67 | $person->name_last = 'Gutmans'; |
---|
68 | $person->favorite_color = 'blue'; |
---|
69 | $person->save(); // this save will perform an INSERT successfully |
---|
70 | |
---|
71 | echo "<p>The Insert ID generated:"; print_r($person->id); |
---|
72 | |
---|
73 | $person->favorite_color = 'red'; |
---|
74 | $person->save(); // this save() will perform an UPDATE |
---|
75 | |
---|
76 | $person = new Person(); |
---|
77 | $person->name_first = 'John'; |
---|
78 | $person->name_last = 'Lim'; |
---|
79 | $person->favorite_color = 'lavender'; |
---|
80 | $person->save(); // this save will perform an INSERT successfully |
---|
81 | |
---|
82 | // load record where id=2 into a new ADOdb_Active_Record |
---|
83 | $person2 = new Person(); |
---|
84 | $person2->Load('id=2'); |
---|
85 | |
---|
86 | $activeArr = $db->GetActiveRecordsClass($class = "Person",$table = "Persons","id=".$db->Param(0),array(2)); |
---|
87 | $person2 = $activeArr[0]; |
---|
88 | echo "<p>Name (should be John): ",$person->name_first, " <br> Class (should be Person): ",get_class($person2),"<br>"; |
---|
89 | |
---|
90 | $db->Execute("insert into children (person_id,name_first,name_last) values (2,'Jill','Lim')"); |
---|
91 | $db->Execute("insert into children (person_id,name_first,name_last) values (2,'Joan','Lim')"); |
---|
92 | $db->Execute("insert into children (person_id,name_first,name_last) values (2,'JAMIE','Lim')"); |
---|
93 | |
---|
94 | $newperson2 = new Person(); |
---|
95 | $person2->HasMany('children','person_id'); |
---|
96 | $person2->Load('id=2'); |
---|
97 | $person2->name_last='green'; |
---|
98 | $c = $person2->children; |
---|
99 | $person2->save(); |
---|
100 | |
---|
101 | if (is_array($c) && sizeof($c) == 3 && $c[0]->name_first=='Jill' && $c[1]->name_first=='Joan' |
---|
102 | && $c[2]->name_first == 'JAMIE') echo "OK Loaded HasMany</br>"; |
---|
103 | else { |
---|
104 | var_dump($c); |
---|
105 | echo "error loading hasMany should have 3 array elements Jill Joan Jamie<br>"; |
---|
106 | } |
---|
107 | |
---|
108 | class Child extends ADOdb_Active_Record{}; |
---|
109 | $ch = new Child('children',array('id')); |
---|
110 | $ch->BelongsTo('person','person_id','id'); |
---|
111 | $ch->Load('id=1'); |
---|
112 | if ($ch->name_first !== 'Jill') echo "error in Loading Child<br>"; |
---|
113 | |
---|
114 | $p = $ch->person; |
---|
115 | if ($p->name_first != 'John') echo "Error loading belongsTo<br>"; |
---|
116 | else echo "OK loading BelongTo<br>"; |
---|
117 | |
---|
118 | $p->hasMany('children','person_id'); |
---|
119 | $p->LoadRelations('children', " Name_first like 'J%' order by id",1,2); |
---|
120 | if (sizeof($p->children) == 2 && $p->children[1]->name_first == 'JAMIE') echo "OK LoadRelations<br>"; |
---|
121 | else echo "error LoadRelations<br>"; |
---|
122 | |
---|
123 | $db->Execute("CREATE TEMPORARY TABLE `persons2` ( |
---|
124 | `id` int(10) unsigned NOT NULL auto_increment, |
---|
125 | `name_first` varchar(100) NOT NULL default '', |
---|
126 | `name_last` varchar(100) NOT NULL default '', |
---|
127 | `favorite_color` varchar(100) default '', |
---|
128 | PRIMARY KEY (`id`) |
---|
129 | ) ENGINE=MyISAM; |
---|
130 | "); |
---|
131 | |
---|
132 | $p = new adodb_active_record('persons2'); |
---|
133 | $p->name_first = 'James'; |
---|
134 | |
---|
135 | $p->name_last = 'James'; |
---|
136 | |
---|
137 | $p->HasMany('children','person_id'); |
---|
138 | $p->children; |
---|
139 | var_dump($p); |
---|
140 | $p->Save(); |
---|
141 | ?> |
---|