-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Better way to extend BaseBuilder #2428
Comments
Being able to extend the database classes is a known issue. Something that will be addressed down the road for sure. If you look over the Query Builder docs there's a number of places where you can use subqueries as an anonymous function. I take it these don't work for your use case? |
This will be very useful to be supported properly but there are a lot of issues how to handle this properly. Problem is that database builders extends the BaseBuilder an if you need to extend it you need to extend the Builder for specific database For example for MySQL you'll have to extend CodeIgniter\Database\MySQLi\Builder |
The only idea that comes on my mind at the moment is to use Trait for the extensions methods and extend each of the database builders so they use the Trait and use the extended classes for querying the db |
I can't discern how this is different than Query Grouping (https://codeigniter4.github.io/CodeIgniter4/database/query_builder.html?highlight=subquery#query-grouping) that Lonnie linked. @najdanovicivan or @Martin-4Spaces can you elaborate on what MySQL output you would like from sub-queries that is not covered? The larger issue of |
Hey @MGatner, following your question, I'm stumbling on a limit with the query builder: sub queries are only implemented for the |
@yassinedoghri See #5510 |
Please submit feature requests to our forum. |
I'm extending the functionality of CI4's BaseBuilder to handle sub-queries like this:
In this use case, i'm trying to fetch all users who I have not already "starred".
Anyway, the use case is not important. The idea is to combine multiple queries. This feature is not that hard to accomplice.
Imagine a function
public function selectSubQuery($query, $alias)
where$quey
is an instance of Model.Step 1: Replace
${parent}
with the table name of$this
in all$query->binds
Step 2: Merge
$query->binds
with$this->binds
Step 3:
$sql = $query->compileSelect()
Step 4: Do some str_replacing in
$sql
to handle conflicting table namesStep 5:
$this->select("{$sql} AS {$alias});
I've implemented this feature in my own code and it works. The issue is BaseBuilder is not extendable. So I cannot modify the binds-array in a proper way. I have to create my own DB-driver to extend BaseBuilder. Furthermore, I cannot call
compileSelect
in BaseBuilder because it is a protected method.My issue is solved for now by copying a database driver and make the changes there. But I think we should be able to extend more database classes. :)
The text was updated successfully, but these errors were encountered: