• 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Integrating relationship query builder similar to Laravel

#1
Lightbulb 
So I hate to compare frameworks, but I love Laravel's relationship query builder. I actually integrated this into my CodeIgniter 4 installation and I would love to share it with others. So far, I did the more complex relationship (BelongsToMany). I have it setup almost exactly the same way as Laravel, but using CodeIgniter's Builder. However, I ran into one issue where I needed the QBSelect property to be public or have a getter method for this in the BaseBuilder so I just adjusted this inside the vendor directory which isn't the best thing to do, but I really didn't want to override all the Database, BaseConnection, Postgre (the driver I'm using) Connection, and all those other classes I would have to extend and create instances of inside the connect() Config/Database method. So for now, I made this small one liner change.
I am working on creating a repository for everything I have so far. I adjusted the way attributes are set on the model as well so they are now nested inside the attributes property which when casted to array or json, the __get and __set will use those attributes. I also added scopes into my query builder so now you can call a method directly on your model and it will check for a scopeMethodName() and pass the builder instance into the first parameter similar to what Laravel has.
If anybody is interested, I would love to share my code with you. I have everything inside my App\Models directory right now and am using my own BaseModel class that extends CodeIgniter's Model class. And it provides all this functionality on top of it.
What does everybody think about this?
I'm working on the other relationships right now Smile . But I have pivot columns and everything working for the BelongsToMany where you can specify what pivot columns you want if any!
Reply

#2
That doesn't sound like a feature request - sounds more like you have code you want to share. Which is awesome!

I'd love to see it when you get it ready for everyone. Would be loved by many in the community I believe.

Instead of modifying core code, you can always submit a pull request to the CI repo and explain why you need that public (a getter would be preferred here...) and we'd likely accept it. Especially to enable a package like this.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#3
(07-25-2019, 09:41 AM)kilishan Wrote: That doesn't sound like a feature request - sounds more like you have code you want to share. Which is awesome!

I'd love to see it when you get it ready for everyone. Would be loved by many in the community I believe.

Instead of modifying core code, you can always submit a pull request to the CI repo and explain why you need that public (a getter would be preferred here...) and we'd likely accept it. Especially to enable a package like this.

Fantastic! I'm anticipating having this done by next weekend after I get all the other relationships hooked up! I would love this to become available for others as it really helps speed up writing code to query relationships.
And I'll go ahead and do that either this evening or tomorrow since I have been keeping my dependencies up to date with the latest alpha version and would hate to have to keep reminding myself to make it public Tongue. And I agree! A getter would be better!
In the mean time, I'll make sure to keep my code up to date so it's easy to extend this Model instead of CI's Model and it will still work seamlessly!
Reply

#4
I’m also very interested to see this! Share a link when you have something up?
Reply

#5
(07-27-2019, 05:09 AM)MGatner Wrote: I’m also very interested to see this! Share a link when you have something up?

dito. I am interested too.
Reply

#6
Great! I am interested too.
Reply

#7
Alright so I got the public git repo up so we can start testing it out and working out all the kinks and getting it to a place where it can be very helpful to developers who love developing in CodeIgniter! Smile Then maybe in the future, I could add a PR to the main CI4 repo if it becomes a hit!

https://github.com/hlohrenz/CI4-Relation...ry-Builder

Let me know what you all think <3 Feel free to clone it and test it out. I have tested all relationships (not the HasManyThrough), but I may have missed a few bugs so I would love if everybody messed around with it! I'm still working on adding more functionality. My main thing is adding the whereHas() method to models to add constraints depending on a constraint on the related model. Smile
Reply

#8
(07-30-2019, 12:07 PM)hlohrenz Wrote: Alright so I got the public git repo up so we can start testing it out and working out all the kinks and getting it to a place where it can be very helpful to developers who love developing in CodeIgniter! Smile Then maybe in the future, I could add a PR to the main CI4 repo if it becomes a hit!

https://github.com/hlohrenz/CI4-Relation...ry-Builder

Let me know what you all think <3 Feel free to clone it and test it out. I have tested all relationships (not the HasManyThrough), but I may have missed a few bugs so I would love if everybody messed around with it! I'm still working on adding more functionality. My main thing is adding the whereHas() method to models to add constraints depending on a constraint on the related model. Smile
Went through it, it's lovely, for now, it would be lovely making it a composer package for flexibility.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.