#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Ithaca
    Posts
    68
    Rep Power
    2

    About Model and ORM in MVC


    Well I've learned quite a bit of MVC pattern thanks to the books I got about Codeigniter and CakePHP. Right now there's one thing I dont quite understand about Model in MVC when it has an ORM system.

    Is the concept Model equivalent to Domain Model with ORM? Or is Domain Model just one specific type of Model?

    And if so what are the connection of DataMapper and Identity Object with Model? Is DataMapper pretty much a 'factory' object responsible for generating Models?

    Sorry for the mess, I am just a bit confused with how ORM works in a MVC framework.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,871
    Rep Power
    6351
    The M in MVC is the data model layer. The data model layer can be whatever you want it to be (assuming it still conforms to the rules of modeling the data and not the business or view), but most people end up using an ORM to model their data.

    The Domain Model of ORM is most closely related to the Controller in MVC. When discussing "pure" ORM implementations, there's the Data/Persistence Model and the Domain Model. The Domain Model is one layer "up" from pure data and contains business logic. In MVC, the Controller contains business logic.

    Though you could make your Model layer a 2-part layer. Symfony encourages this behavior by having "pure" ORM objects which cannot be altered, as well as having an "override" object where you can put additional business logic which doesn't quite fit in a controller (or needs to be reused a lot).

    I'm not even sure what DataMapper is, are you maybe reading from a specific book or website? Both ORM and MVC are patterns, not products. There's no "ORM" thing you can download and get the same "ORM" that everyone else has.

    In general, MVC and ORM describe two different things. MVC is a pattern used to develop an entire web app. It describes how to organize the "layers" of your application so that the types of code (data access, business, display) are separate. In MVC, the "Model" layer is simply "how you get to your database."

    ORM, on the other hand, describes ONLY how you get to your database. It's one of many patterns of database access. ORM has expanded in definition a bit to include business logic in the objects, which is why you got confused about the Domain Model and how it fits into MVC.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Ithaca
    Posts
    68
    Rep Power
    2
    Thanks for such detailed reply, I appreciate it very much. Guess DomainModel is kinda different from the traditional concept of Model, but well even Models in each framework behaves diversely. I wonder how Symfony manages to fit ORM into MVC, the sourcecode of Symfony looks quite complex. I know I do not need to know how its working behind the scene to use a framework properly, but as a PHP programmer I just cant help getting curious. XD

    And DataMapper is a special type of object that maps information from relational database to domain model objects. It provides another level of abstraction, although at the cost of significant overhead.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,871
    Rep Power
    6351
    I wonder how Symfony manages to fit ORM into MVC, the sourcecode of Symfony looks quite complex.
    Symfony has a very strict MVC, but the Model layer can contain business logic. The layers of Symfony in particular:

    - Base data. (5 classes per table) Auto-generated off your database schema and do nothing but access and associate data objects.

    - Extended data. (2 classes per table) Auto-generated at first, but then ignored. These classes inherit from the base data tables and allow you to extend functionality to include additional logic for validation, association, dependencies, etc. Some business logic ends up here, though it's technically wrong.

    - Controller. (1 class per site module) You can auto-generate a "bundle," which does some of the initial annoying work for you and creates the folder tree, the translation files, the routing file, and other metadata files which aren't PHP, plus an empty controller file. Business logic goes here, as well as the logic for preparing data for the views, processing form posts, validation, etc.

    - Views. (1 twig file per page) Twig is very simplistic and can't do much but spit out the data that the controller gave it.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Ithaca
    Posts
    68
    Rep Power
    2
    I see, Symfony does look like a quite professional framework, everything is well standardized and mostly automated. I personally dont mind if Model Layer has business logic, this gives them certain degree of flexibility. Thanks for the great summary of Symfony, I guess I understand pretty much how it works now. Still wonder how it does work behind the scene though, but perhaps I aint good enough to interpret everything yet.

IMN logo majestic logo threadwatch logo seochat tools logo