Associations in CakePHP

When I first started using CakePHP I was stuck for about 3 to 4 days with associations. After scouring the web, reading probably about 100 articles I have realized they are quite confusing when you first start with out using cake. I am hoping this post assists other people who were in my position for the past 3 days.

Associations are used for those tasks where you need to look up a row or multiple rows in another table based on an id number of sorts. The first thing to do in CakePHP is to make sure you have all the conventions correct to the very last point. If you want to make it really easy on yourself also I would recommend following their database naming conventions also. Here is a mini checklist:

  • Model file names, var $name, and class names are singular
  • Controller class names and var $name are plural also the file name is the plural version of model name followed by _controller.php
  • The folder name for a view is plural and the file name should match the “action” name or function name in the controller followed with the extension .thtml
  • Every table in your database should be the plural form of the model name.
  • The primary key of each table should be id
  • The associated key should be the name of another table singularly followed by _id (eg. I have a table named event_catogires, then in events my associated column name should be called event_category_id)

As far I can think right now. I think that may be all the key ones. If you have all the above correct the rest should be a breeze. I will now show you how I got something like a category name to appear next to a title of an article. This example isn’t meant to be copied. I don’t give enough information for that, but I hope this leads you guys in the right direction.

The Model:

class Event extends AppModel
var $name = 'Event';
var $useTable = 'events';
var $belongsTo = array('GalleryCategory' , 'EventCategory');


We are saying Event $belongsTo or $hasOne of each of these models (‘GalleryCategory’ , ‘EventCategory’). When find is called in the controller it will look up the following models and bring up that single row we are looking for in each of these.

The Controller:

class EventsController extends AppController
var $name = 'Events';

function index()
$this->set('events', $this->Event->findAll());
$this->set('title', 'All Events');

Now I won’t show the view because it doesn’t really matter. You could just enable scaffolding if you had wished. $this->set(‘events’, $this->Event->findAll()); set up a variable named events so that in the view we can retrieve an array with all database fields. This includes the rows that were found set up in the belongs to of the events model. You will need to make a model and controller for every related table.

On the reverse side. Let us say you wanted to show all events with a event_category. Then all you need to do is simply use the following line rather than belongsTo:

var $hasMany = 'Events';

There are other associations, but I feel that these 2 would be the most used. For example if you had something like tags which unlike categories have and belong to many pages you would use: var $hasAndBelongsToMany = ‘Events’;.

The most import thing to get this stuff to work is following the conventions. If you don’t everything falls to pieces somethings work, somethings don’t and you will have holes in your system.

If you have any questions feel free to post a comment. I am a beginner with cake myself, but I am hoping by the end of august I will be a pro haha.


Leave a Reply

You must be logged in to post a comment.