Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using query builder and ORM
I'm using ORM in my project and so far it's working great. However I now need to write some large queries with many joins and subqueries and would rather use the query builder for this. What's considered best practice when using ORM and the query builder? Should I just add these queries to functions in the ORM models?
Moved to the ORM forum. Use the best tools for the job. Use ORM's standard methods when you want to interact with the objects, use DB queries for complex or mass-update work. As the model is your interface to the database table, I would add these queries to methods in the model. Note that ORM models keep an internal cache of all data objects retrieved, to make sure all models operate on the same data. If you modify the database via DB queries, the data in the database might not be the same as the ones in cache, which potentionally could lead to dangerous situations. Jelmer, Is there a way for model methods to flush the cache in cases like this? Maybe it's a good idea to introduce a flush() method for the ORM model class?
What WanWizard said, for specialized queries and heavy DB operations the QB will often be the better choice. The way I solve this is to add methods to my model (static or dynamic) that do these things using the QB instead of the ORM. WanWizard:
It's never fully possible, all solutions will be half baked as PHP doesn't allow manual object destruction. That means that all relations and any other references wouldn't be replaced on a new fetch from the DB. Thus flushing the cache is useless. Only thing that might be done different is to make it update the cache, the downside of that is the additional overhead and danger of overwriting unsaved changes.
Thought so, that's why I asked. Maybe we should add this as a note to the docs, as it might lead to odd side effects for those not in the know (like you've just run an update query, then access a model and get old data back...).
Thanks, I appreciate the help Thanks also for building this brilliant framework. Now my favourite PHP framework
Add a Comment
It looks like you're new here. If you want to get involved, click one of these buttons!
Apply for Membership
↳ Installation & Setup
↳ Tips and Tutorials
↳ Code share
In this Discussion