My schema looks like this:
items - item_id, ...
items_item_types - item_id, item_type_id
item_types - item_type_id, ...
I need a query that will get items with ALL the item types given. For example, let's say item 1 is type 10, and item 2 is types 10, 11, and 12. I'm given 10 and 11, item 1 wouldn't be returned but item 2 would.
To clarify further:
--------------------------
| item_id | item_type_id |
| 1 | 10 |
| 2 | 10 |
| 2 | 11 |
| 2 | 12 |
--------------------------
I can do this easily in SQL, for example
select * from items
join item_item_types join_table1 on items.item_id = join_table1.item_id and join_table1.item_type_id = 10
join item_item_types join_table2 on items.item_id = join_table1.item_id and join_table2.item_type_id = 11
Which would return item 2 in this case
The query is already quite complicated and I'm being passed a reference to the QueryBuilder object, so I cannot use native SQL (as far as I know). What my code looks like:
function addItemTypesToQuery(&$qb, $itemTypes) {
foreach($itemTypes as $key => $value) {
$qb->join('item.itemTypes', 'item_type' . $key, 'WITH', ???)
}
}
I'm not sure what the join condition needs to be to mirror the SQL. Doctrine skips over the intermediary step of joining the item_item_types table but I need to set join conditions on THAT, not on the item_types table.