You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello.
There are two entities product and filters related by many-to-many. Needed to filter the goods through the pivot table.
The following code does everything necessary, but the generated query contains a double join, at a time when to filter it is enough to join with the pivot table.
finalclass ApplyProductFilters
{
publicfunction__invoke(Select$select, array$filters): Select
{
return$select->where(staticfunction(Select\QueryBuilder$q) use ($filters) {
foreach ($filtersas$filterID => $filterValue) {
$q->orWhere(staticfunction(Select\QueryBuilder$q) use ($filterID, $filterValue) {
$q->where('[email protected]', $filterID);
if (is_array($filterValue)) {
$q->andWhere(staticfunction(Select\QueryBuilder$q) use ($filterValue) {
if (count($filterValue) == 2 && is_numeric($filterValue[0]) && is_numeric($filterValue[1])) {
$q->orWhere('[email protected]', 'between', $filterValue[0], $filterValue[1]);
} else {
foreach ($filterValueas$value) $q->orWhere('[email protected]', $value);
}
});
} else {
$q->andWhere('[email protected]', $filterValue);
}
});
}
});
}
}
SELECT `product`.`id` AS `c0`, `product`.`name` AS`c1`,`product`.`price` AS `c2`, `product`.`attributes` AS `c3`, `product`.`description` AS `c4`, `product`.`created_at` AS `c5`,
`product`.`category_id` AS `c6`, `product`.`brand_id` AS `c7`
FROM `products` AS `product`
INNERJOIN `filters_products` AS `product_filters_pivot`
ON `product_filters_pivot`.`product_id` = `product`.`id`
INNERJOIN `filters` AS `product_filters`
ON `product_filters`.`id` = `product_filters_pivot`.`filter_id`
WHERE ((`product_filters_pivot`.`filter_id` = ? AND (`product_filters_pivot`.`filter_value` = ? OR `product_filters_pivot`.`filter_value` = ? ) )OR (`product_filters_pivot`.`filter_id` = ? AND (`product_filters_pivot`.`filter_value` BETWEEN ? AND ? ) ) )
LIMIT ? OFFSET ?
I tried calling the join directly from the Cycle\ORM\Select instance. But this way adds a master table prefix before the name of the table to be joined.
roxblnfk
changed the title
Double join when filtering many-to-many relations.Cycle\Orm\Select adds a master table prefix before the name of the table to be joined in MtM case
Sep 7, 2023
@msmakouz
About multiple joining - it's correct (see #397 (reply in thread)).
The main point is Select adds a master table prefix before the name of the table to be joined
Hello.
There are two entities product and filters related by many-to-many. Needed to filter the goods through the pivot table.
The following code does everything necessary, but the generated query contains a double join, at a time when to filter it is enough to join with the pivot table.
I tried calling the join directly from the
Cycle\ORM\Select
instance. But this way adds a master table prefix before the name of the table to be joined.Originally posted by @Shelamkoff in #397
The text was updated successfully, but these errors were encountered: