开发者

How to best structure CakePHP Models

I'm new to CakePHP and am looking to implement the best practice for CakePHP models. I'm still开发者_开发知识库 googling through results, but was wondering if you had any suggestions on how to structure things or had any links to tutorials...


The Database interaction functionality must be placed in models. e.g. saving data, saving relative data, validating, Abstraction of relation between models...

in other words all heavyweight stuff that's unique to the given model.

All functionality that is not directly associated to the given model or may be used in many models has to be stored in behaviors.

Controllers should be very light, and easy to read. they must control business workflow using model's functionality and also it's own.

And remember keep your models fat and controllers skinny ;).

P.S.

cake's cookbook is good to start.


Here you can find the manual paragraph regarding models. If you need more specific answers about how the API works with Models you could try the official API page. Also I've googled and found repetitive articles about this couple of Fat Model and Skinny Controller practice you could find useful.

As a general answer, Models are:

Models represent data and are used in CakePHP applications for data access.

Basically, with Models you manage database data and show them in the controller (considering the controller also as a glue between View, Controller and Models). The following words best describe the pratical use of a model:

A model usually represents a database table

So that if you have a user table you should create at least one model to control everything you want to control of the database user table. You could for example access it with User::create($name, $password, $email) or User::edit($id, $set, $value) and modify database user related tables.


A model is reference to a specific set of data. It can be in the database, a text file, or any other type of data. The key to remember is that a single model should reference a single data set. To keep the example simple, I will demonstrate a users table. Let's assume we have the following table in the database:

CREATE TABLE `users` (
  `id` char(36) NOT NULL,
  `email_address` varchar(127) NOT NULL,
  `password` varchar(40) NOT NULL,
  PRIMARY KEY (`id`)
);

The bare bones basic model for this table will look something like:

<?php
class User extends AppModel {
    var $name = 'User';
    var $displayField = 'email_address';
}

Now the key to the MVC architecture (and CakePHP) is that any function required for USER data manipulation should be contained within this model. That is a bit broad and sweeping. There may be some instances where it would occur elsewhere. But if you can always think of things in terms of data or business requirements / processes, it will help you determine where the code should go.

So for the users model, if you need to ADD a user, the model will handle that addition. The controller will call the form (the view) and then process the form sending the data to the model to be added to the database.

So if you wanted to check for unique email addresses before adding a record to the database, it should be done in the model. If you want to confirm the the email address is valid, it should be done in the model. If you want to have a password verification where the user enters two passwords and you check them to make sure they are identical, you would do that in the model.

This is what is meant by fat model skinny controller. Anything that could fit in the model, probably should go there.

Hope this helps.


Abstract view:

It can be difficult to apply this fully in Cake, but an instance of a model (in the MVC sense) represents an entity, rather than a database table or just a data access layer.

For example, an instance of the User model represents a particular user.

For this to work, your model methods must assume that the data for a particular entity has been loaded or set:

/**
 * Send an SMS to user
 *
 * @precondition $this->data has been set
 * @param string $msg
 * @return void
 */
public function sendSms($msg) {
    $this->smsGateway->send($this->data['User']['mobile'], $msg);
}

The precondition indicates that User->read() must have been called before this method will work correctly. You might think this is a bit anal for a loosely typed language but the advantage is that your models start to look more like the real world they are modelling, and by making assumptions and sticking to them you can simplify your code === win.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜