In MVC-Model View Controller design you implement the model to work seperately and contain buisness logic, pulling information down from the database.

I struggle so much with the design of implementing a good model. I know what information needs to be pulled down from the database, I just don't know the best way to implement it. I think of the model as the program API and I overload myself with questions like

  1. What if I need to sort the fields?
  2. What if I need to select by a certain username/id?
  3. What if I need to group by a particular field?
  4. How badly will the performance be affected if I'm selecting * just incase the calling function might need any of the information pulled down?

My API/Model becomes extremely bloated, having seperate functions and queries (just slighlty tuned/changed) for each function

For example


I feel this makes the model extremely bloated and very tied, creating a lot of duplicated code. It might be better having this kind of model;

A better idea

$cart->getItems('title, price')->order_by('title');

Where 'title, price' is the mySQL fields you can select, obviously being validated by the getItems() function. That way it's not only restricted to returning certain fields.

  1. How could I achieve this?
  2. Is this truly a good model?
  3. Are there any other things you guys could suggest?

Accepted Answer

Use object-relational mapping (ORM)...

Try the Doctrine ORM project.

Another solution is CodeIgniter, it has the best active records library out there. Very helpful.

If you still decide to write own class then use PHP5's method chaining. Syntax would be prettier...

Written by Otar
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the community.
It is licensed under cc-wiki