Question

I'm wondering what the preferred approach for creating/naming models is?

My application has a "User" model, which contains all the relevant business logic for a creating, fetching, updating (etc) a User record.

In some of my controllers, I may want to fetch a list of multiple "User" records.

In this case, should I create a whole new model called Users?

The way I see it, there are a few options:

  1. I can give database access to the controller, and the controller can query all the relevant users, then create a User model object as I loop through them. (BAD)

  2. I can have two separate models, "User", and "Users". User object will handle creating, fetching, updating single records, and Users object would really only be useful for listing all the User records.

I don't like the idea of doing something like this:

$user = new User();
$user->fetchAll();

Purely for semantic reasons. A User object should relate to just one user record, in my opinion.

Maybe I'm thinking about this the wrong way, and my "User" model, which relates to the "users" table in the database, should really have been named "Users" from the start.

How do you deal with this issue?

Was it helpful?

Solution

Hmm, just try to be consistent I guess.. For me I will use User model to interact with table User, so I don't really care about how many records I will interact with..

Check this question: going to build a php MVC, what naming conventions do i need to be aware of?

OTHER TIPS

You don't want a separate model for representing singluar or plural versions. Just one or or the other. You can have a collection of user instances, but that's a collection (array), not a "Users" class.

You can semantically call your model whatever you want, really, but singular seems to be the accepted norm.

Basically the way you should think about it is that a User model represents a single "user," so your user service/repository's findAll method should return a collection/array of singular user entities/models.

I think you have the right idea there. In order to maintain single responsibility, your User() class should not be involved with the fetching and updating of the table. It should merely represent a single user, and it's functions should be directly related to that. If you start having your User() functions and the functions of a table mixed together, you break Single Responsibility. For this reason, I go with the second approach, and that's how it seems to work in the frameworks I've worked with

In Zend Framework, classes that extend Zend_Db_Table handle the table manipulation, and return classes that extend Zend_Db_Table_Row. In Zend Framework, Zend_Db_Table_Row has a save and delete method, which calls back to the table to do the actual saving and deleting. I think this stays in line with sing responsibility, as you consider that responsibility to do with that row directly, not just a model separated from the row. Still, you have two classes. A table class and a row class

In Doctrine (I'm new to this, so I'm not familiar with advanced usages), each model represents a single row in the table, even so far as defining what columns are in the row. The rows are fetched and updated and created by the Doctrine Repository. If you require a repository just for your Table, then you do that but it's a separate class from User()

To sum up, to keep in line with Single Responsibility, I'd have UsersTable()::fetchAll() return User[]. Personally, I'd have User be able to call on UsersTable to update or delete the row, but that's simply a personal preference.

I have been banging my head against the wall for the past few days over the same thing.

I would have preferred to keep a single class myself but I guess this is where OOP can be slightly limited.

I would say if you just want one class then I would go with Plural because it does not make semantic sense for a User to return multiple users,

I know this has already been answered but I will contribute my 2 cents:

class UserModel
{
    /**
    * @var $user
    */
    public $user;

    function __construct()
    {

    }

    public function get_all()
    {
         //Get all products
    }

    public function get($where)
    {
         //Get user instance here either by:
         $this->user = $result; //Result from SQL Row
         //or:
         return new User($result);
    }
}

class User
{
    public $firstname;
    public $lastname;
    public $somevar;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top