2

I'm quite confused on how do I build my models, I failed to understand this for the last 9 months. Although I am reading and watching all the references , @teresko gave to me.

To further narrow down my question, I'll give an example of how I did this from before.

Lets say I have a student entity that has student_number, first_name,last_name`

I will then create my so called model, (my professor did the same, but I know this is quite wrong). I don't know if my professor knows Value Objects, but I do.

private $student_number;
private $first_name;
private $last_name;

public function setStudentNumber($sn) {$this->student_number = $sn}
public function getStudentNumber() {return $this->student_number}
... and so on for other properties

If I am correct, this setters and getters are classified as a Value Object pattern, that can be used like this:

$s = new Student();
$s->setStudentNumber(143);
$s->setFirstName('FooName');
$s->setLastName('BarName');

And pass it in a Data Access Object (StudentDAO) like this:

$sDao = new StudentDAO($s);
$sDao->add();

Where the DAO extends the Database Class, so I can do CRUD for example.

The question is, well, I'm pretty sure I will get a lot of scolding here about I missed too many principles, but what are those? How do I create my models? Thank you! Well, I know many answers will tell about DataMappers, Factories and stuffs, which I cannot understand very well.

Joey Hipolito
  • 3,108
  • 11
  • 44
  • 83
  • 1
    From Zend page definition of model: `Model - This is the part of your application that defines its basic functionality behind a set of abstractions. Data access routines and some business logic can be defined in the model.` – Robert May 14 '13 at 11:54
  • There are many ways to abstract the data access. Read [this](http://stackoverflow.com/questions/3198419/orm-dao-datamapper-activerecord-tablegateway-differences) post and follow the links to martinfowler's blog. – bitWorking May 14 '13 at 11:57
  • 1
    Possible duplicate of: http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc Top answer contains the titles **What is NOT a model?** and **What IS a model?** – Jimbo May 14 '13 at 11:58
  • 1
    Also see [thin controller fat model](http://stackoverflow.com/questions/3109715/understanding-mvc-whats-the-concept-of-fat-on-models-skinny-on-controllers). So your model should contain the business logic and not the controller. – bitWorking May 14 '13 at 12:04

2 Answers2

3

Model:

  • Dao: Connects to your datasource (i.e. file, database/sql, webservice etc).
  • Mapper: Maps external data from your Dao to your internal Entities/Domain objects (getters/setters), and internal data to external.
  • Service: Business logic.

Controller:

  • action calls Service layer (not Mapper or Dao)

Dependency: Service -> Mapper -> Dao i.e. Dao is injected into Mapper, Mapper is injected into Service

This way you could change your datasource from a database to a webservie & your business logic would stay the same.

NB: I recommend, the Dao & Mapper both have an interface

Eddie Jaoude
  • 1,698
  • 15
  • 23
1

As you are emphasizing on you don't know about lots of Principles, I would suggest to look at http://symfony.com/ and try to use doctrine (as using doctrine with standalone project would be difficult) for model layer. (or some other similar MVC framework) Once you have idea how you use them, you will start to get principle of those. Symfony is easy to setup to run a basic web application and has got good documentation.

Webghost
  • 933
  • 2
  • 7
  • 18