Hi! I’m trying to use Gas ORM but I’m stuck with a many-to-many relationship.
I’ve two tables A, B linked by a third C table
A fields: [id, name]
B fields: [id, name]
C fileds: [id, a_id, b_id, value]
on C table, a_id and b_id are FK to related tables.
What I’m trying to do is to get for each A item, his name, the name of related B items plus the value from C table.
E.g.
John, Red, 10
John, Blue, 12
Mike, Red, 11
This is how I’ve defined relationships
In A model:
self::$relationships = array(
'a' => ORM::has_many('\\Model\\B\\A => \\Model\\A')
);
In B model:
self::$relationships = array(
'b' => ORM::has_many('\\Model\\B\\A => \\Model\\B')
);
In C model:
self::$relationships = array(
'b' => ORM::belongs_to('\\Model\\B'),
'a' => ORM::belongs_to('\\Model\\A'),
);
Then I get some data using this code:
$data['items'] = Model\A::with('b')->all();
So I can use it into view
foreach ($items as $a) {
foreach ($a->b() as $b) {
// Here I need to show the value field from C table
}
}
And now the question, does this query $data[‘items’] = Model\A::with(‘b’)->all(); retrieves C table data too?