Welcome Guest, Not a member yet? Register   Sign In
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
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#4

(This post was last modified: 03-28-2020, 02:10 PM by dave friend.)

(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.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
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.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#8

(This post was last modified: 02-19-2021, 09:20 AM by PwrSrg.)

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.

Bridging the gap between Technical and Creative

VASCOsoft Web Studio   |   AllMyLinks

Reply




Theme © iAndrew 2016 - Forum software by © MyBB