Welcome Guest, Not a member yet? Register   Sign In
Using MVC

[eluser]Paul Scott[/eluser]
Morning All,

I am just about to start playing about with CodeIgniter in a project I'm going to be working on. I'm going to see if I can get a good enough feel for it to use it and learn it on-the-fly.

My main worries about using CodeIgniter is that I struggle with sticking to the MVC structure (even though CodeIgniter is apparently not very strict about it) because I'm not quite sure how to work this.

I understand that Models are used for interaction with the database, Views are used for outputing the HTML and Controllers pull the page together. I'm not really sure how to port parts of my code, specifically classes that I have written (which, yes, interact with the database).

Lets say I'm using the following (mock) class:
class Member {

    var $id, $name, $email;

    function Member($id=NULL) {
        if ($id !== NULL)

    function load($id) {
        // ...
        $this->id = $r['id'];
        $this->name = $r['name'];
        $this->email = $r['email'];


Before using MVC I would do the usual
$member = new Member($id);
and as far as my understanding goes the `Member` class should be a model because it's main function is to take things from the database. What's confusing me is that with CI I load models using `$this->load->model('member')`. So how would I change my class to work with the format `$member = new Member($id)`, assuming that I want to be able to use the CI database functions and having multiple instances of the `Member` object (this is why I think I should be using `new` over `$this->member`).

I've had a read around the forums and I believe what I'm looking for is
$this->CI =& get_instance(); // off the top of my head
but I'm looking for a little more insight into how what I'm wanting to do fits into the MVC workings.


This is perhaps not such a direct reply but here goes:

Sometimes when you port sites to CI, you'll have classes that don't exactly fit, or worse, can't be transformed into MVC pattern without a crapload of work and violation of the Don't Repeat Yourself rule. I put these things into the application/libraries folder and autoload them. Works wonders.

For example, I had a class calledd Api that contained about 40 methods all concerning database queries (f. example: getProject($id), deleteProject($id), ...)

Instead of trying to forge it into models, I just renamed the class to Data and put it in my libraries. Works without having to change a thing in the code. Now I just call $this->data->getProject($id). You'll have to load the database class before the Data class though, in the autoload array Smile.

Hope that gives a little insight. (it all depends on the magnitude of what you are trying to port, ofcourse)

[eluser]Paul Scott[/eluser]
Thanks for that info. It doesn't exactly answer my queries because the situation is slightly difference. My code (I think) does fit into MVC - I'm just not sure how to work with using multiple instances of (what I believe should be) a model class.

Edit: Perhaps that wasn't especially clear in my original post. Thanks for your input, all the same.


I think there is a similar discussion in this thread


[eluser]Paul Scott[/eluser]
I found what I was looking for in the documentation (funnily enough, eh?)

I guess I just over-looked it when I was originally trying to find what I was after - doubtful that it would be in there anyway - but I found it at http://ellislab.com/codeigniter/user-gui...aries.html

Thar be some good documentation with CodeIgniter. Might just need to print it all off and read it on the bus to/from work.

Thanks for input

[eluser]Matthew Pennell[/eluser]
[quote author="deviant" date="1187709254"]I think there is a similar discussion in this thread

Indeed. The answer to the OP is that Code Igniter doesn't think of models as "an instance of an object representing a record from the database" - models are just places to put your functions that deal with database access.

I've written an Active Record implementation that is closer to what you're talking about - I'll post more about it soon.

Theme © iAndrew 2016 - Forum software by © MyBB