A while ago I wrote an article Thinking about the model in which I shared some ideas about how CakePHP’s Model class could be improved. In this article I want to share another idea for the Model class.
If you look at the Model class you will see that it represents two different “things”: on the one hand it represents a single record, and on the other hand it represents a table (or to be more generic: a container of records). That’s not that clean from an object-oriented point of view, and hence I think it would make sense to split the Model class into two parts, e.g. Record and Container. All properties and methods related to a single record would be in the Record class, and all table-oriented functionality in the Container class (see also the “Managers” section in the Django book, which describes a very similar concept). In practice it could look like:
$post = $this->Posts->findById(1); // Posts is a Container which returns a Post object (a Record class) $post->delete(); // delete the post with id 1
Maybe this is something for CakePHP 2.0?