6

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..??

3
  • I am still really confused on the whole MVC thing, but as I understand it, your setter (with Model) method is really just a way to push objects of a model class into the DB, so it should be a method on the model, and probably called by the controller.
    – Anthony
    Commented Apr 19, 2012 at 7:52
  • in .NET world we'd typically call the MVC models 'view models', while the ORM entities would typically compose the domain model.
    – MattDavey
    Commented May 28, 2012 at 13:36
  • @user1175327 what structure did you used in the end?
    – johnlemon
    Commented Apr 1, 2013 at 13:45

4 Answers 4

1

I would not call myself a backend developer, so before I answer, I will strongly encourage you to spend some time looking at how others are doing MVC architecture. I think CakePHP is a great place to start (and their code is very well documented!).

The way I would set out about doing it is to make a /Utilities folder that other parts of your program can call on when needed. Things that you might often be using in multiple controllers, views or models. You could place standard validation rules -- notEmpty(), alphaNumeric(), email(), etc. -- in this file (/Utilites/Validation.php).

Then you might make a /Models/Model.php that has your basic model template (save, delete, update, beforeSave, etc.) that would let new models inherit from it so you don't have to copy/paste repeated lines of code everywhere. Then you could make your userModel and if necessary have custom validation inside of that file.

Again, not a back end developer but I am familiar with CakePHP and looking out how they decided to handle these difficulties might give you some ideas!

1

Just rename "Repositories" "Validations" or "Rules" and it all makes sense again.

0

I'm slightly confused at the first question. They're actually working with data retrieval and storage, but they don't commit data anywhere? Or are you saying they don't actually store an object inside of the class so you shouldn't call them a Repository? If it's because they don't actually store an object you could rename the directory to DataAccess or something similar.

If the files inside of the Entities directory are only doing validation you could rename that to Validation or ValidationRules.

I hope that helped you a bit, but really as long as the name of the directory accurately describes what the files inside of it do you'll be fine.

0

There are different approaches regarding to the structure, and decision is made based on how much abstraction you want.

If you want to separate the validation and data model classes, you can put the data classes in Entity directory and validation classes under Validation directory, both under Model directory.

You can put the search methods under Repository directory if you are using a separate repository class. Or you can combine it with the data class.

Different frameworks and different ORM systems have different conventions. You may want to check the framework and ORM documentation regarding best practices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.