• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter Model VS Query Builder

#1
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
Reply

#2
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.
Reply

#3
That’s because you can call the query builder directly from the model class : https://codeigniter4.github.io/userguide...ry-builder
Test your translation files with Translation Tester
Reply

#4
(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...ry-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.
Reply

#5
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.
Test your translation files with Translation Tester
Reply

#6
(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.
Reply

#7
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.
Test your translation files with Translation Tester
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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