CodeIgniter Forums

Full Version: Another MVC newbie question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]medvind[/eluser]
I have a model called "Order" that corresponds to a row in a database table named order.

If I would like to list all rows in this table from a controller, am I supposed to make another model (Order_list or something like that) or should I use the Order model? Is it important for the model to correspond to a row in the database? What's the best practice in this case?

El Forum

[eluser]davidbehler[/eluser]
In general I would have a model for each controller, e.g. controller "order" and model "order_model".

In your model you could have these functions
Code:
function get($id)
{
  //get row with id = $id
}
function get_list()
{
  //get all rows
}

Basically you can do whatever you want ^^

El Forum

[eluser]jedd[/eluser]
And some people would have one model to every database table.

A third group will go for somewhere in between.

In the same way that there is no cake, there is also no Right Way of doing this.

[quote author="medvind" date="1250101358"]I have a model called "Order" that corresponds to a row in a database table named order. [/quote]

Your Order model sounds like it should actually correspond to your order table, and so you'd create new methods whenever you want to do something with that table.

El Forum

[eluser]medvind[/eluser]
Thanks for the quick replies. So this is a matter of preference, after all...

El Forum

[eluser]Johan André[/eluser]
I usually end up with one model per databasetable.
Sometimes a model can interact with several tables.

Let's say I have a table that holds different stores:

Code:
id          int(11) auto_inc PRIMARY_KEY
name        varchar(255)
city_id     int(11)

...and maybe a table that holds all my cities (referenced by the city_id-field in the stores-table:

Code:
id          int(11) auto_inc PRIMARY_KEY
name        varchar(255)

I would make the store-model use both the stores and cities-tables.

As jedd said, theres nothing right or wrong.
Try to group similar or connected functionality is my go.

Good luck!

El Forum

[eluser]medvind[/eluser]
Tack Johan!

"Try to group similar or connected functionality" <- good idea