Wednesday, July 16, 2008

Fat Models, Skinny Controllers

What is a fat Model?
  • When I say fat, I'm not talking about the FAT filesystem here - I mean long lines of code, methods, variables, etc. Basically, a fat script would be a big script.

Why should I make fat Models vs skinny Controllers?
  • After developing for awhile, when you look back at your code you'll realize how hard it is to keep track of things. Basically, with a skinny controller (and meaningful function names) you can quickly see what's going on. When you have a controller with 300 lines of code, it can get pretty messy looking (even with proper formatting)...So to solve that problem, simply put many of your functions in your Models instead.

What type of functions should I put in my Model?
  • The type of functions you should put in your model should be anything related to your model, or it's data. Formatting, Retrieving, Searching are just a few examples.

What type of functions should I put in my Controller?
  • Usually, the only functions you should have in your controller are: Index, Add, Edit, View, Delete, and the "before" functions. Almost anything else can go in your model.

When I first started with CakePHP, I put almost everything in my controllers. In reality, the controllers should be extremely tiny - and only contain important business logic. As I worked with CakePHP more, I also learned to start putting more in my Models and less in my Controllers.

To put a function in your Model, you do it the exact same way as putting it in your controller:

function getEventDetails($_id) {
$params = array(
'conditions' => array(
'Event.id' => $_id,
),
);
return $this->find('first', $params);
}
To use the function, inside your controller use the following format:
$this->ModelName->functionName();

Here's an example:

$eventDetails = $this->Event->getEventDetails($_id);
After adding functions to your models, you can also access those functions using Model associations if they're set up correctly:
If you have a User Model and an Event Model that are associated,
you could put this in a method in your Users Controller:

$eventDetails = $this->User->Event->getEventDetails($_id);

Monday, July 14, 2008

CakePHP Plugins

Plugins were a great idea added to CakePHP 1.2 that allows you to create a mini web application inside your main web application. This mini web application you can then distribute to other users, or your other projects, to reduce re-inventing the wheel so much.

Plugins were a great idea, but is not quite something the CakePHP core team really focuses much attention on, and probably never will. Despite that, CakePHP plugins can still be very useful for decreasing code time between projects - as long as you remember the pitfalls associated with them.

Some very important things to remember when working with Plugins:

  • When naming controllers, try to make their names unique (to prevent any ambiguity errors if two controllers have the same name) Usually prefixing any filename with the plugin's name works great (such as calendar_events_controller.php where calendar is the plugin name)
  • Every controller in your plugin must be told where to find its models. Even though you're using the $uses variable, still try to stick with CakePHP's conventions of having 1 model per controller. To tell your controller where your model is, use the following:
    • var $uses = array('PluginName.ModelName');
  • When working with model associations in a plugin, never use the short form (eg. $hasMany = ModelName); always extend it like the following examples(!Notice how className has PluginName.ModelName, where the ModelName before that is by itself!):


var $hasMany = array(
'ModelName' => array(
'className' => 'PluginName.ModelName',
)
);

and

var $belongsTo = array(
'ModelName' => array(
'className' => 'PluginName.ModelName',
),
);

below are two examples taken from two different models:
Special Note: The key in the array doesn't contain the plugin name but the className part does

var $hasMany = array(
'CalendarAttendantsEvent' => array(
'className' => 'Calendar.CalendarAttendantsEvent',
'dependent' => true,
)
);

var $belongsTo = array(
'CalendarAttendant' => array(
'className' => 'Calendar.CalendarAttendant',
'foreignKey' => 'calendar_attendant_id',
),
'CalendarEvent' => array(
'className' => 'Calendar.CalendarEvent',
'foreignKey' => 'calendar_event_id',
),
);


Using the above examples, I haven't had any troubles working with associated models in my plugin, but any other way I've tried wouldn't work properly in some cases (such as accessing a model's function through associations with $this->ModelName->functionName(); )

Also, please see the manual for more information: http://book.cakephp.org/view/114/plugins