CakePHP & MySQL Workbench relations

To start designing databases for CakePHP application firstly you need to be aware of distinctions between database relation types in MySQL  and model associations in CakePHP.

MySQL tables relationship CakePHP objects association
one-to-one non-identifying relationship hasOne
one-to-many non-identifying relationship hasMany, belongsTo
one-to-one identifying relationship not supported*
one-to-many identifying relationship not supported*
many-to-many identifying relationship hasAndBelongsToMany

* As there is no multiple-column primary key support in CakePHP it can be said that there is no support for 1:1 identifying and 1:n identifying relationships. The only exception is associated model with one column primary key which is relation foreign key at the same time

Screenshot below presents simple EER diagram (I have highlighted the Workbench relationships creator area in red):

Relationship description MySQL tables relationship CakePHP objects association
Each user may have only one profile page.
Each profile page is associated with one user.
users <-> profile_pages
(one-to-one non-identifying relationship)
User hasOne ProfilePage
Each article can be commented many times.
Each comment is created by a User.
Each user can create many comments.
articles <-> comments
(one-to-many non-identifying relationship)

users <-> comments
(one-to-many non-identifying relationship)

Article hasMany Comment,
Comment belongsTo Article,

User hasMany Comment,
Comment belongsTo User

Each user can like many articles.
Each article can be liked by many users.
users <-> users_articles <-> articles
(many-to-many identifying relationship)
User hasAndBelongsToMany Article

As I mentioned before it’s worth to use CakePHP naming convention during the design process. Once you’ve got proper database schema you can easily generate plain source code of your application. In other words MySQL Workbench + CakePHP code generator can be used as ORM (Object-relational mapping) design tool.

Via Rocketmill

CakePHP uploads linked to Models

How to upload files such as images for a certain Model in CakePHP, you ask?

Get the Upload plugin

Simply download the following plugin to your /app/Plugin/ folder, and make sure the new folder in there is called ‘Upload’.
Enable the plugin in your /app/Config/bootstrap.php;


You can also just follow the Installation instructions on the Plugin website.

Use the plugin in your Model of choice

In the model you want to have uploads, create a database field called ‘photo’ (varchar 255) and add the following code to your Model;

    public $actsAs = array(
        'Upload.Upload' => array(
            'photo' => array(
                'thumbnailSizes' => array(
                    'vga' => '285x285',
                    'thumb' => '80x80'
                'thumbnailMethod'  => 'php',
                'fields' => array(
                	'photo' => 'photo',
                	'dir' => 'uploads',
                	'type' => 'photo_type'

Then in the View of the add/edit page, add the following code:

echo $this->Form->create('Note', array('class' => 'form-horizontal', 'type' => 'file'));
echo $this->Form->input('', array('type' => 'file', 'label' => 'Foto'));
echo $this->Form->input('message', array('label' => __('Bericht')));
echo $this->Form->end(); 

That’s it!

By default, the new uploads are saved in the /app/webroot/files/{modelname}/photo/{model_entry_id}. You can change this in the options in the Model.
More about those options here.

Happy coding!

Fat Models, skinny Controllers

I think that’s quite a lovely design pattern!

The controller isn’t there to do everything. It is there to determine what needs to be done, and have other parts of code do it.

You keep the overview in the Controller, so that the Controller only needs to do small alterations to posted data or direct the data to the Model, which can handle it accordingly.

The Controller basically is used as a manager..

Thanks for the data View, handle it Model!

Small example of the functions M & C should do:


  • Handle GET & POST data
  • Logical alterations of the data provided by either the Model or View
  • Passing data from View to Model
  • Passing data from Model to View


  • Save data to the database
  • Retrieve data from the database
  • Handle caching
  • Handle custom functions to manipulate database