0

I have struggled with an MVC concept for a long time in php since I'm not a php developer. I refactored my site several times but it still looks like a cannon pointed in a mosquito. I've read many articles, posts and answears about MVC but it still makes me confused.

Let's have a simple website with articles and comments system. Everything is stored directly in a database (information about articles, paths, comments etc.). Mostly based on this I developed my own app using MVC. Here is how it works:

  1. Controller retrieves data from the database about an article and related comments
  2. It creates required model's objects (current article and comments)
  3. ... and then passes necessary data to the View
  4. View presents those data by applying particular templates for provided objects

Everything works like a charm but...

  • my model's objects don't need any external logic so my domain objects are bordered to getter/setter classes -I'm wondering: do I need domain objects? It's pointless to store separated objects only for getting them from DB and displaying on a website after a while, isn't it?
  • controller holds a DB connection passed to services and mappers for creating a model's object -but queries stored in mappers can be merged into services functionality

In conclusion, I wonder if it's not a better approach to replace three-part model with a single manager (per object) which will retrieve all necessary data from the database and will pass them in any way and form (i.e. associative array) to the View.

Please do note, that I'm describing a simple case (1:1:1) with tiny logic, where most data retrieved from database are only for a presentation purpose.

Community
  • 1
  • 1
Luke
  • 2,539
  • 2
  • 23
  • 40
  • 1
    MVC architectural pattern is not meant for small projects. You will just get additional bloat. MVC is meant to be used in codebases where simply following best OOP practices is not enough anymore to contain the complexity, at which point you apply MVC pattern which imposes additional set of constraints. – tereško Dec 30 '14 at 14:40
  • My small project overgrew because of applying model layer "the right way". I think I will merge a model layer the way I described in the question. It will still remain expandable (for future needs) but will eliminate unnecessary overgrowth at the point such complexity isn't required. Thank you. – Luke Dec 31 '14 at 09:14

1 Answers1

0

Well what you have just described is an architectural pattern for data modelling called the Data Mapper pattern.

In this pattern you separate the persistence logic to a Mapper class and leave all of the Data stuff in The Domain Model. In your scenario you mentioned there is little logic, which is fine. Logic grows as your application matures. The key point is the separation, persistence has nothing to do with logic and therefore should be moved into a different class.

The Mapper class is only concerned with persisting data (generally to whatever data store you choose), in practice you uses "Adapters", which is why in some ORMs you see a different adapter for different data stores (ie. MySQLAdapater vs PostGresAdapater... etc, implementing a abstract Adapter Interface/Base Class).

It should be noted though, in your conclusion what you describe is actually the MVC Architecture.

The Model Layer is essentially your Persistence Layer (like described above) The View Layer is where you present your data. Your Controller Layer is where your data manipulation ie On your domain object's data.

Typically a MVC request goes like... a URI gets mapped to a controller::action Action gets data, manipulates it and sends it to the view for rendering.

When MVC applications run into trouble is when your Controller accesses several different objects and needs to persist them all. The class architecture of an MVC application tends to be very heavy as the Models typically have more logic on them than in the Data Mapper pattern since MVC models have persistence tied into them (in practice).

Just as an aside, for simple projects like yours, you might want to look into simple presentation frameworks like http://www.slimframework.com/ It has no persistence layer built in, which leaves you to choose the best for your project.

geggleto
  • 2,605
  • 1
  • 15
  • 18
  • 1
    There are no "models". Model (singular) is a layer. Not a class or object. Controllers do not contain application logic and are not aware of persistence layer, but instead they only alter the state of model layer. And views are not dumb templates, but instead contain all of the UI logic. – tereško Dec 30 '14 at 19:46
  • Basically, this answer is **completely wrong**. You should stop "learning MVC" from badly written frameworks. – tereško Dec 30 '14 at 20:10
  • This logic you mentioned, in my project atm is limited to load/save data from/in a database. In general, yes -logic should be separated from mappers but I want to avoid juggling between additional and unnecessary classes. Such granulation is not necessary in my opinion for small projects. Thank you. – Luke Dec 31 '14 at 09:34