This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi,
I'm struggling to get a hasManyThrough to work and I'm not sure if a) hasManyThrough is right to use or b) I'm just not grasping it properly.
Basically I have three tables
table_1, table_2 and table_3
table_1 has an item_id table_2 has a user_id and an item_id table_3 has a user_id
I want to get all items in table_3 from the table_1 model.
The raw sql query would be something like this
select `table_3`.*, `table_2`.`user_id` from `table_3` inner join `table_2` on `table_2`.`user_id` = `table_3`.`user_id` where `table_2`.`id` = user_id_from_related_table_1_item_id
My hasManyThrough thus far.
public $hasManyThrough = [
'items' => ['table_3', 'through' => 'table_2', 'primaryKey' => 'item_id', 'throughKey' => 'user_id']
];
I'm not sure if primaryKey should be item_id or user_id, but either way it doesn't work. The resulting query is:
'select `table_3`.*, `table_2`.`user_id` from `table_3` inner join `table_2` on `table_2`.`id` = `table_3`.`user_id` where `table_2`.`user_id` is null and `table_2`.`id` = ?
(? being the item_id from table_1)
Can anyone shed any light?
Cheers,
Adam
Last updated
1-1 of 1