orm icon indicating copy to clipboard operation
orm copied to clipboard

Left join for select

Open Eugentis opened this issue 4 years ago • 2 comments

A lot of times during the development we need to load some nullable refs with LEFT JOIN. In current state Cycle Select don't provide any simple method for this.

Now it is required to write smth like this:

abstract class AbstractRepository extends Repository
{
    public function leftJoinLoad(Select $select, array $joinsList): Select
    {
        foreach ($joinsList as $ref => $alias) {
            $select
                ->with($ref, ['method' => Select\JoinableLoader::LEFT_JOIN, 'as' => $alias])
                ->load($ref, ['using' => $alias]);
        }

        return $select;
    }
    ...
}

And it is not so comfortable)

Eugentis avatar May 13 '21 10:05 Eugentis

You can use method in load method as well, have you tried it?

wolfy-j avatar May 13 '21 10:05 wolfy-j

@wolfy-j Yes I am tried it and it has not suitable results for grid in keeper for example:

SELECT `client`.`email`                                      AS `c0`,
       ...,
       `l_clientCase_updatedBy`.`email`                                  AS `c49`,
       ...
FROM `cases` AS `clientCase`
         INNER JOIN `users` AS `clientCase_updatedBy` ON `clientCase_updatedBy`.`uuid` = `clientCase`.`updated_by`
         ...
         LEFT JOIN `users` AS `l_clientCase_updatedBy` ON `l_clientCase_updatedBy`.`uuid` = `clientCase`.`updated_by`
ORDER BY `clientCase_updatedBy`.`first_name` ASC, `clientCase_updatedBy`.`last_name` ASC
LIMIT 20

It is example for sorting by nullable field and as you can see it build 2 Joins

My implementation from description build required sql:

SELECT `clients`.`email`                    AS `c0`,
       
       ....
       `updatedByUser`.`email`                         AS `c49`,
       ....
FROM `clients` AS `clients`
         ...
         LEFT JOIN `users` AS `updatedByUser` ON `updatedByUser`.`uuid` = `clientCase`.`updated_by`
ORDER BY `updatedByUser`.`first_name` ASC, `updatedByUser`.`last_name` ASC
LIMIT 20

Eugentis avatar May 14 '21 05:05 Eugentis