3

I have some trouble getting a good folder structure in my project and i would like to know what other ways i could use to structure my files.

I'm currently working in an MVC sturctured folder.

www/
  Controllers/
  Models/
  Views/

Nothing special so far. But i'm also using an ORM system. With it i can easily get an 'object' from my database like:

ORM::load('table');

Now this sort of code should reside in a Model right? So i'd get something like this:

<?php
class userModel
{
    public function getAllUsers ( )
    {
        return ORM::load('table');
    }

    public function getUserById ( $id )
    {
        return ORM::load('table', 'userid=?', array($id));
    }
}
?>

Looks good so far in my opinion... But there's one more thing. I can also specify a 'model' when using the ORM system. With this model i can basically set up validation rules. Like so:

ORM::withModel('authModel');

This lets the ORM know that before it adds a new row (or updates an existing one) to the DB, that it should check the following model first for validation rules.

<?php
class authModel //Or maybe authValidation??
{
    // Method gets automatically triggered when an update is done with the ORM
    public function onUpdate ( $obj )
    {
        if ( $obj->username == '' )
            throw new \Exception('No username');
    }

    public function onInsert ( $obj )
    {
        // Validations here too.
    }
}
?>

Now the problem is, is that i have 2 sorts of models. One where i basically use getters/setters to get and store data to the database (from my controller to my model).

And i have another model in which validation rules are set... I don't want to mix both in the same folder. So i must come up with another structure for this. Something like:

www/
  Controllers/
  Models/
    Repositories/
    Entities/
  Views/

It's just that my model isn't a real 'repository', since it doesn't store any objects in the repo class and doesn't have a commit() method or anything like that.

I also can't store the 2nd model (for validations) in the Entities folder, because they're not Entities at all...

Any idea how i should structure this..??

Vivendi
  • 16,485
  • 22
  • 93
  • 175
  • Why don't you use some ready to go farmeworks with ORM like Kohana? – s.webbandit Apr 16 '12 at 07:34
  • 7
    @webbandit Because i'm creating my own 'framework' to gain some more experience. I know i could easily use other frameworks. But i'd like to learn things this way too, by doing it myself. – Vivendi Apr 16 '12 at 07:56

1 Answers1

10

The first thing you should understand is that the Model in MVC is not a class/object. It is a layer, made from a multitude of objects. I'm too lazy to do the same song'n'dance again , so just read this comment (skip to the "side notes" section).

The root of your confusion lies in the fact that you recognize two different responsibilities in the group of classes you call "models". You are actually having class instances responsible for business logic ( like your UserModel class ), and a separate thing called "ORM", which loads and stores content. And you have authentication, which does not fit in either of the groups.

I would go with a structure like this:

/application
    /modules
        /public
            /controllers
            /templates
         /admin
            /controllers
            /templates
         ....
    /views
    /model
        /logic
        /persistence
        /services
/framework

You might notice that there is a separate /views folder in /application and also each module has a separate /templates. That is because, in proper MVC the Views are instances of classes, responsible for presentation logic and usually juggle multiple templates. If well written, they too are a reusable structure.

And the final note: Do not try to use ORMs. Make a datamapper for each domain object which requires it. Some people consider ORMs to be antipatterns. Also, avoid static calls .. thats not OO code. You could benefit a lot from learning about dependency injection

.. my 2 cents

Community
  • 1
  • 1
tereško
  • 56,151
  • 24
  • 92
  • 147
  • Although i must disagree on the part that an ORM is truly an anti pattern (some say the same about the repo pattern for example). I don't think it is bad at all. I'll still be using it. But that's a whole other discussion. But you gave me some good insights on the structural part :-) – Vivendi Apr 17 '12 at 10:16
  • 1
    @Vivendi , I just hope that you read the article about ORMs , not just dismissed it as something you disagree with as a matter of principle. – tereško Apr 17 '12 at 11:49