Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to define join aliases in Active Record #2096

Closed
1 task done
jcogs-design opened this issue May 24, 2022 · 1 comment · Fixed by #2097
Closed
1 task done

Add the ability to define join aliases in Active Record #2096

jcogs-design opened this issue May 24, 2022 · 1 comment · Fixed by #2097

Comments

@jcogs-design
Copy link
Contributor

Describe the solution you'd like
Sometimes it is necessary to use an alias in a JOIN - when you want to make two separate table joins to the same column for example. Active Record does not support this, nor does it support the addition of aribtrary SQL to a query it is building, so currently you cannot (for example) use a lookup table multiple times in the same request. For example (from the MySQL docs you might want to do this

SELECT t1.name, t2.salary
  FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name;

Currently this is not possible to specify using Active Record' join() method.

It would be good to add the ability to specify an alias in the join() method.
It would be easy enough to add to the function by defining a further optional parameter to those already in place, but it would be the necessary to disable the application of the _protect_identifiers() method to the join condition, since otherwise AR would attempt to (at the very least) prefix the alias prefix (similar to the effect of setting $escape = false in a select() statement).

Describe alternatives you've considered
Usually the need for aliased joins can be avoided by data table design (e.g. avoiding the need for look-ups by duplicating data), but if you are working with legacy data this might not be possible.

Teachability, Documentation, Adoption, Migration Strategy
A small addition would be needed to the AR documentation to note the additional parameter, and that table-name protection is removed when an alias specified.

  • I am capable and would like to work on implementation of this feature if it is considered.
intoeetive added a commit that referenced this issue Aug 15, 2022
Added support for table alias in SQL joins when using Active Record, #2096
intoeetive added a commit that referenced this issue Aug 17, 2022
Added support for table alias in SQL joins when using Active Record, #2096
@intoeetive
Copy link
Contributor

This has been merged and released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants