MainBlog Archives
Recent Posts |
Hello boys and girls Again, a small fix in ActiveRecord on HookTable Support.. Here is how to use it: first some classes: class foo extends ActiveRecord { function initTable() { $this->table = 'foo'; } function initRelations() { $this->hasMany(new bar(), 'hook_bars'); $this->hasMany(new line(), 'hook_lines'); $this->hasMany(new sinker(), 'hook_sinkers'); $this->hasMany(new test()); } } as you can see, hasMany now holds a second optional parameter.. the second parameter is a string, holding the hooktable name.. related classes: class bar extends ActiveRecord { function initTable() { $this->table = 'bar'; } } class line extends ActiveRecord { function initTable() { $this->table = 'lines'; } } class sinker extends ActiveRecord { function initTable() { $this->table = 'sinkers'; } } class test extends ActiveRecord { function initTable() { $this->table = 'tests'; } } And some sample code:
$db = mysql_connect("localhost", "user", "pass"); mysql_select_db("db", $db); include_once("class.ActiveRecord.php"); include_once("class.foo.php"); include_once("class.bar.php"); include_once("class.line.php"); include_once("class.sinker.php"); include_once("class.test.php"); $foo = new foo(); $foo->setAttrib('id_foo', '1'); $foo->get(true); Ok.. the tables hold this: mysql> select * from foo; +--------+-----+ | id_foo | foo | +--------+-----+ | 1 | foo | +--------+-----+ 1 row in set (0.00 sec) mysql> select * from bar; mysql> select * from lines; mysql> select * from sinkers; and some hooktables:
mysql> select * from hook_bars; +----+--------+--------+ | id | id_foo | id_bar | +----+--------+--------+ | 1 | 1 | 1 | | 2 | 1 | 2 | +----+--------+--------+ 2 rows in set (0.00 sec) mysql> select * from hook_lines; mysql> select * from hook_sinkers; and the onetomany directlinked test table: mysql> select * from tests; +---------+--------+--------+ | id_test | id_foo | test | +---------+--------+--------+ | 1 | 1 | test 1 | +---------+--------+--------+ 1 row in set (0.00 sec) so.. when getting foo with id_foo=1 and relations to bar, line, sinker and test with this hooktable, we get 'bar1', 'bar2', 'line 123', 'sinker 321' and 'test 1' linked to foo with id_foo = 1. The rest.. well.. they behave like normal relations quite simple huh have fun using it.. Matthijs Thanks for listening for now.. Sourcecode is available
mtempels | General | 23 01 2006 - 11:36
|