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'); |
---|
15 | $db->debug=1; |
---|
16 | ADOdb_Active_Record::SetDatabaseAdapter($db); |
---|
17 | |
---|
18 | $db->Execute("CREATE TEMPORARY TABLE `persons` ( |
---|
19 | `id` int(10) unsigned NOT NULL auto_increment, |
---|
20 | `name_first` varchar(100) NOT NULL default '', |
---|
21 | `name_last` varchar(100) NOT NULL default '', |
---|
22 | `favorite_color` varchar(100) NOT NULL default '', |
---|
23 | PRIMARY KEY (`id`) |
---|
24 | ) ENGINE=MyISAM; |
---|
25 | "); |
---|
26 | |
---|
27 | class Person extends ADOdb_Active_Record{} |
---|
28 | $person = new Person(); |
---|
29 | |
---|
30 | echo "<p>Output of getAttributeNames: "; |
---|
31 | var_dump($person->getAttributeNames()); |
---|
32 | |
---|
33 | /** |
---|
34 | * Outputs the following: |
---|
35 | * array(4) { |
---|
36 | * [0]=> |
---|
37 | * string(2) "id" |
---|
38 | * [1]=> |
---|
39 | * string(9) "name_first" |
---|
40 | * [2]=> |
---|
41 | * string(8) "name_last" |
---|
42 | * [3]=> |
---|
43 | * string(13) "favorite_color" |
---|
44 | * } |
---|
45 | */ |
---|
46 | |
---|
47 | $person = new Person(); |
---|
48 | $person->name_first = 'Andi'; |
---|
49 | $person->name_last = 'Gutmans'; |
---|
50 | $person->save(); // this save() will fail on INSERT as favorite_color is a must fill... |
---|
51 | |
---|
52 | |
---|
53 | $person = new Person(); |
---|
54 | $person->name_first = 'Andi'; |
---|
55 | $person->name_last = 'Gutmans'; |
---|
56 | $person->favorite_color = 'blue'; |
---|
57 | $person->save(); // this save will perform an INSERT successfully |
---|
58 | |
---|
59 | echo "<p>The Insert ID generated:"; print_r($person->id); |
---|
60 | |
---|
61 | $person->favorite_color = 'red'; |
---|
62 | $person->save(); // this save() will perform an UPDATE |
---|
63 | |
---|
64 | $person = new Person(); |
---|
65 | $person->name_first = 'John'; |
---|
66 | $person->name_last = 'Lim'; |
---|
67 | $person->favorite_color = 'lavender'; |
---|
68 | $person->save(); // this save will perform an INSERT successfully |
---|
69 | |
---|
70 | // load record where id=2 into a new ADOdb_Active_Record |
---|
71 | $person2 = new Person(); |
---|
72 | $person2->Load('id=2'); |
---|
73 | |
---|
74 | var_dump($person2); |
---|
75 | |
---|
76 | $activeArr = $db->GetActiveRecordsClass($class = "Person",$table = "persons","id=".$db->Param(0),array(2)); |
---|
77 | $person2 =& $activeArr[0]; |
---|
78 | echo "<p>Name (should be John): ",$person->name_first, " <br> Class (should be Person): ",get_class($person2); |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | ?> |
---|