Main

Blog Archives

Recent Posts

rss

Hello boys and girls face-smile.png

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);

echo '<pre>';
print_r($foo);
 

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;
+--------+------+
| id_bar | bar  |
+--------+------+
|      1 | bar1 |
|      2 | bar2 |
+--------+------+
2 rows in set (0.00 sec)

mysql> select * from lines;
+---------+----------+
| id_line | line     |
+---------+----------+
|     123 | line 123 |
+---------+----------+
1 row in set (0.00 sec)

mysql> select * from sinkers;
+-----------+------------+
| id_sinker | sinker     |
+-----------+------------+
|       321 | sinker 321 |
+-----------+------------+
1 row in set (0.00 sec)

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;
+----+--------+---------+
| id | id_foo | id_line |
+----+--------+---------+
1 |      1 |     123 |
+----+--------+---------+
1 row in set (0.00 sec)

mysql> select * from hook_sinkers;
+----+--------+-----------+
| id | id_foo | id_sinker |
+----+--------+-----------+
1 |      1 |       321 |
+----+--------+-----------+
1 row in set (0.00 sec)
 

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 face-smile.png check out my previous post HERE face-smile.png

quite simple huh face-smile.png

have fun using it..

Matthijs

Thanks for listening for now.. Sourcecode is available
http://jaws.townsville.nl/download/class.ActiveRecord.phps <-- HERE!

mtempels | General | 23 01 2006 - 11:36