CodeIgniter Forums

Full Version: [SOLVED]How should I use my Model(s) ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]julien.1486[/eluser]
Hello !

I'm a newbie in MVC conception and I have a problem with my application.
I'm writing a small application in which users suggests me to add new features to the website.
I have two tables in my database : category and features.

category : id (primary key) int, name varchar(50)
feature : id (primary key) int, name varchar(50), description text, status varchar(50), idcat (foreign key to category.id)

And I would like to have this kind of result for instance, in a view :
Code:
- Blog
|------ Add an article (done)
|------ Delete an Article (not yet)
|------ etc....
- Members
|------ Add a sign-up (done)
|------ Sending a confirmation email (done)
|------ etc....

How should I organize my code ?

I have this model but I think it's not the good way :

Code:
class Category extends Model
    {
        function Category()
        {
            parent::Model();
        }
        
        function getAll()
        {
            $query = $this->db->get('Category');
            return $query->result();
        }
        
        function getFeatures($index)
        {
            $query = $this->db->get('Feature')->where('idcat', $index);
            return $query->result;
        }
    }

And my Controller looks like this :
Code:
class Roadmap extends Controller
    {
        function index()
        {
            $this->load->helper('html');
            $this->load->model('category');
            
            $data['categories'] = $this->category->getAll();
                        
            $this->load->view('roadmap', $data);
        }
        
        
    }

How can I pass to the view the categories with THEIR features ?

Should I write a features model too ? Or should I add a private member $myFeatures as a array ? (But I can't keep the getAll() function : logicaly a Category cannot retrieve all categories (it's rather a collection's function).

It's the first time I use the MVC design patter and my brain is not formatted yet !

Thanks a lot for your help

El Forum

[eluser]julien.1486[/eluser]
Self-reply :

Finally I was in a wrong way.

I make a Model for each table in my database, except for the one including a foreign key (well, I just have two tables for the moment Big Grin).
I've added private members to my class and a array named 'myFeatures'.
When I retrieves the datas from the database I fill 'myFeature' with the result of a query build by using $this->db->get_where(....).
This is described in this topic (thanks to the authors and to the helpers) :
http://ellislab.com/forums/viewthread/88156/

I look for this all the day !

El Forum

[eluser]thurting[/eluser]
Tell them to undress and proceed to take pictures.

El Forum

[eluser]julien.1486[/eluser]
hum ?