First up, sorry for the mega-posting of code but...
I have the following DB structure (not my choice):
+---------+ +--------------+ +--------+
| users | | users_groups | | groups |
+---------+ +--------------+ +--------+
| id | | user_id | | id |
| name | | group_id | | name |
+---------+ +--------------+ +--------+
I've created a relationship with FuelPHP's ORM:
class Model_User extends Orm\Model
{
protected static $_properties = array(
'id',
'name',
);
protected static $_has_many = array('usergroup');
}
class Model_Usergroup extends Orm\Model
{
protected static $_table_name = 'users_groups';
protected static $_properties = array(
'user_id',
'group_id',
);
protected static $_belongs_to = array('user', 'group');
}
class Model_Group extends Orm\Model
{
protected static $_properties = array(
'id',
'name',
);
protected static $_has_many = array('usergroup');
}
And I'm now trying to get a query to display all the data from the 3 tables using the following:
$data['users'] = Model_User::find()
->related('usergroup')
->related('usergroup.group')
->get();
It's returning this:
Array
(
[1] => Model_User Object
(
[_is_new:Orm\Model:private] =>
[_frozen:Orm\Model:private] =>
[_data:Orm\Model:private] => Array
(
[id] => 1
[name] => somename
)
[_original:Orm\Model:private] => Array
(
[id] => 1
[name] => somename
)
[_data_relations:Orm\Model:private] => Array
(
[usergroup] => Array
(
)
)
[_original_relations:Orm\Model:private] => Array
(
)
[_view:Orm\Model:private] =>
[_iterable:protected] => Array
(
)
)
)
So it's starting to find the users_groups but stops? Am I even getting at this in the right way? Of course I could get this with a regular query with joins, but it would be ace to actually know how to do this with the ORM.
Gaaaak! It was just missing a PK on the users_groups table...
class Model_Usergroup extends Orm\Model
{
protected static $_table_name = 'users_groups';
protected static $_primary_key = array('pk_id');
protected static $_properties = array(
'pk_id',
'user_id',
'group_id',
);
protected static $_belongs_to = array('user', 'group');
}