Welcome Guest, Not a member yet? Register   Sign In
Just what is a "Model" supposed to be in CI?
#5

[eluser]mitchwilson[/eluser]
To answer the original post, great question! The CI Model was really confusing for me at first as well, since it did not conform to my expectations based OOP.

From previous programming experience, I expect the model to be a class that I use to make instances. I expect an instance to refer to one record in the database, if I am using a database table for that model class.

Basically, I expect to write code like this and it would load each instance from the database:
Code:
// Create two instances, each would refer to each record in the friends table.
// They would not necessarily automatically connect to the database, but when
// I wanted to connect, they would already know which records they referred to.
$friend1 = new Friend('Sawyer');
$fiend2 = new Friend('Jack');

// Then be able to use each instance to call methods pulling internal information to the model.
$friend1->greet(); // Says 'My name is Sawyer, I am 30 years old.'
$friend2->greet(); // Says 'My name is Jack, I am 43 years old.'

// And I could call a setter that would update the database record for this instance.
$friend1->setAge(39);
$friend1->greet(); // Says 'My name is Sawyer, I am 39 years old.'

Ok end of dream code. That is not how CI works. In CI, a Model is really a database helper to store all your SQL as individual methods for that Model, for example.

Code:
class Friend_model extends Model {

function greet($id) {
// Fill in code to query database to get text from greeting field from friends table
}

}

Note that you need to pass in the id of the record you want. So you call it like this:

Code:
$this->load->model('Friend_model');
$this->Friend_model->greet(123);

Now that is just odd. I really love CI, but that is not OOP. After I got used to it, I'm still not keen on it, but not a big deal to do this to get more OOp on it. I am testing this technique out.

Code:
$this->load->model('Friend_model');
$friend1 = $this->Friend_model;
$friend1->init(123); // Do the extra step of creating/calling an intialize method.
$friend1->greet(); // Says 'My name is Sawyer, I am 39 years old.'

NOTE: All the above is psuedo code for discussion's sake. Some works, some won't when used literally. It's just for information discussion sake only.

Also, note that in OOP, when you initialize an new instance, you usually are creating a NEW instance. In CI, while you might be creating a new instance you are loading an existing record from the database. So using the new keyword is also confusing, even when trying to be more OOP. Using init() is less confusing but not perfect. Maybe use load()?

So, really, I haven't figured out the best technique, but I'm working on it personally. Would love to hear what others have come up with.


Messages In This Thread
Just what is a "Model" supposed to be in CI? - by El Forum - 05-01-2009, 03:02 PM
Just what is a "Model" supposed to be in CI? - by El Forum - 05-03-2009, 05:22 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 05-03-2009, 07:11 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 05-03-2009, 07:17 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 08-19-2009, 07:53 PM
Just what is a "Model" supposed to be in CI? - by El Forum - 08-20-2009, 05:47 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 08-20-2009, 06:25 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 07:42 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 07:46 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 07:53 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 09:12 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 09:45 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 09:56 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 10:07 AM
Just what is a "Model" supposed to be in CI? - by El Forum - 09-29-2009, 11:55 AM



Theme © iAndrew 2016 - Forum software by © MyBB