CodeIgniter Forums
Codeigniter Model VS Query Builder - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Codeigniter Model VS Query Builder (/showthread.php?tid=75871)



Codeigniter Model VS Query Builder - shinseiten - 03-25-2020

Codeigniter Model and Query Builder.
I am confused about the redundancy of these functions and the fact that they seems to have different ways of manipulating and returning data.

I'm migrating a website from codeigniter 3 to 4 and I found this quite confusing.



P.S: Why codeigniter.com has the "you hit a snag" error?  Big Grin


RE: Codeigniter Model VS Query Builder - dave friend - 03-25-2020

I agree it is confusing. Several Model functions "look" like Query Builder (QB) function but they are not. The fact that QB and Model methods can, in certain cases, be chained together strongly suggests they are all of one class, but they are not.

The developer's only recourse is to be intimately familiar with the API for both QB and Model and to write code with the full understanding in mind.


RE: Codeigniter Model VS Query Builder - includebeer - 03-27-2020

That’s because you can call the query builder directly from the model class : https://codeigniter4.github.io/userguide/models/model.html#working-with-query-builder


RE: Codeigniter Model VS Query Builder - dave friend - 03-28-2020

(03-27-2020, 02:37 PM)includebeer Wrote: That’s because you can call the query builder directly from the model class : https://codeigniter4.github.io/userguide/models/model.html#working-with-query-builder
Yes, I think that was said. Maybe it wasn't stated clearly, but the implication is there. That "feature", plus the Model having functions that look like query builder functions (but are not) is, as the OP said, confusing.

Actually, it's more than confusing it is a rampant disregard for the OOP concept of encapsulation. Why is that an issue? Because it leads to questions like the OP. This is not the first time that questions have been asked about apparently buggy behavior that's not really a bug, but a misunderstanding about what really going on behind the scenes. A developer has to be keenly aware of which calls are Model and which are Query Builder lest they get unexpected results.


RE: Codeigniter Model VS Query Builder - includebeer - 03-30-2020

Why is it confusing? For example find($id) will return the row your are asking. Why does it matter if it’s a call to the query builder, or if it’s a call to a model function, that will then call the query builder and return the result? I see those functions as “shortcut” to accomplish task we need to do often. Like fetching a row with its id.


RE: Codeigniter Model VS Query Builder - dave friend - 03-31-2020

(03-30-2020, 01:27 PM)includebeer Wrote: Why is it confusing? For example find($id) will return the row your are asking. Why does it matter if it’s a call to the query builder, or if it’s a call to a model function, that will then call the query builder and return the result? I see those functions as “shortcut” to accomplish task we need to do often. Like fetching a row with its id.

I recognize that the model class seeks to accomplish a wide variety of tasks developers often need to do and make them simple. However, the fact that you can chain Query Builder methods and the Model’s CRUD methods tends to obscure the class relationships - which leads to confusion. If it's not confusing why do questions keep showing up on various forums along the lines of "Why is $model->like('somefield', 'someThing')->get()->getResult() returning deleted rows?" Obviously, it does matter if it’s a call to the query builder or to a model function. It requires more than a cursory reading of the documentation to truly understand how the various classes work together.


RE: Codeigniter Model VS Query Builder - includebeer - 03-31-2020

It’s true that some things like the soft delete feature are taken care of automatically by the model class and they are not if you only use the query builder. But if you make a query with the query builder then add ->first() to only get the first row, it will exclude the deleted rows. I guess this could be improved for more clarity.


RE: Codeigniter Model VS Query Builder - PwrSrg - 02-19-2021

YES!  FINALLY!  I have the exact same issue.

This is BY FAR the most confusing part of Ci4, and I have STILL not been able to wrap my head around it 6 months later.

Unfortunately, no one has properly answered this yet.  What I am looking for is the PROS and CONS of BOTH.