• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED]How should I use my Model(s) ?

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 :
- 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 :

class Category extends Model
        function Category()
        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 :
class Roadmap extends Controller
        function index()
            $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

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) :

I look for this all the day !

Tell them to undress and proceed to take pictures.

hum ?

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.