Welcome Guest, Not a member yet? Register   Sign In
Model Example is Bad in User Guide
#1

[eluser]Unknown[/eluser]
I Am very new to Code Igniter however I have been taught MVC before, I have been reading through the user guide and let me first say how impressive the documentation is. However I have a real disagreement with the example used on page:

http://ellislab.com/codeigniter/user-gui...odels.html

The example extends the running Blog example which is fine however the following function is concerning:

Code:
function insert_entry()
    {
        $this->title   = $_POST['title']; // please read the below note
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

I believe that the reference to the POST variables whether through $this->input->post or $_POST violates the fundamental ideal of Model in MVC. The model by its very nature should be abstracted from the fact that this is even a web implementation let alone be directly referencing POST variables. This function should take "title" and "content" as parameters so that it may be called from anywhere in the program even if it was not directly related to a POST request.

From my understanding of MVC each layer should be isolated from each other sharing only an interface. This type of function removes any and all benefits of MVC, and is a particularly bad example to show as an introduction to Model. The interface between Model and Controller should pass ALL necessary information across so regardless of what changes as long as that interface is adhered to you do not need to recode anything else.

For example: If I now call that function like so

Code:
$this->Blog_Model->insert_entry()

1) It is not clear what this is going to insert.
2) If this is called from a place where the POST values are not available or not related to what you wish to insert. Your function no longer works.

I believe that this needs correction because it is the first introduction to models given to newcomers.

Regards Boblemur
#2

[eluser]Unknown[/eluser]
I can't agree with you anymore. The model example is too simple and too stupid!
#3

[eluser]jasonjack[/eluser]
THIS WAS SOMETHING NEW FOR ME BUT I THINK I KNOW SOMETHING ABOUT THIS... BUT STILL I KNOW MANY THINGS AB OUT PLAYING GAMES...
#4

[eluser]cjke[/eluser]
I would be interested to see other people's take on this - as I'm reasonably new to the MVC pattern I would like to know the right way before getting in too deep
#5

[eluser]toopay[/eluser]
don't you see, some note according to that example, something like :
Quote:Note: For the sake of simplicity in this example we're using $_POST directly. This is generally bad practice, and a more common approach would be to use the Input Class $this->input->post('title')
#6

[eluser]cjke[/eluser]
Don't you see, that the OP stated that even $this->input->post is a bad idea (which I agree with) as we don't know with any certainty that the input will be pulled from the post data.

My understanding so far is that the model should be an abstraction from the end application implementation. Accessing POST data destroys that abstraction
#7

[eluser]toopay[/eluser]
Quote:...as we don’t know with any certainty that the input will be pulled from the post data.
I think, that reference User Guide, describe it with something like this...
Quote:bla bla...For example, let's say you use CodeIgniter to manage a blog.

Of course, this is just a sily example, as you can read from its notes
Quote:Note: For the sake of simplicity in this example we’re using $_POST directly. This is generally bad practice, and bla bla
This example, if you want to mention it too, even doesn't assume that you use Form Validation Class, which more OOP.

I agree, that the example was to simple or even become bad practice. But, it still show anyone who read it, that CodeIgniter use MVC pattern, to separate your application workflow into :

Model => are PHP classes that are designed to work with information in your database.
Conroller => are PHP classes that contain your Bussines Logic
View => Final Output
#8

[eluser]cjke[/eluser]
I guess my issue is that the example doesn't give a clear cut case of when, and how, a model should be used. It does it is designed to be used with information in your database, but does mean anytime a query is made, it should be passed off to the model?
#9

[eluser]toopay[/eluser]
i agree ;-)
#10

[eluser]InsiteFX[/eluser]
This is why use use a library and you set all of your
input post data in the library then you pass the $data array
to your model!

Very simple!

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB