source: sandbox/2.5.1-evolucao/phpgwapi/inc/adodb/tests/test-active-relations.php @ 8222

Revision 8222, 3.0 KB checked in by angelo, 11 years ago (diff)

Ticket #3491 - Compatibilizar Expresso com novas versoes do PHP

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