Including related fields in CakePHP 3.0

Being a CakePHP newbie I found it hard to find any information on this topic, and the answer is in fact really really simple.

The answer

 $project = $this->Projects->get($id, [
   'contain' => [
     'Estimates' => [
     'Logs' => [

The question

I have a couple of models related to each others; Customers, Projects, Logs, Estimates, Types  and Statuses.

Each customer has serveral Projects.
Each Project has serveral Logs and Estimates and a Status.
A Log and an Estimate has a Type.

I ran  complete out-of-the-box scaffolded CakePHP app, and all worked fine. In the single project view I also saw related Logs, but the Type field for each Log showed an integer – the type_id. I wanted instead to show the Type name, but found it really hard to find how.

This is where the answer above comes in handy. The documentation for CakePHP 3.0 mentions how to add table names to the contain-parameter, but I haven’t found any mention of it being possible to nest multiple tables inside the parameter.

Take the following example:

 $project = $this->Projects->get($id, [
   'contain' => [
     'Estimates' => [
     'Logs' => [

Here I’m in my ProjectsController’s view function. By default it contains all tables directly related to the current project (Customers, Estimates, Logs, Statuses). In my view I can use $project->customer->name to get the name of the customer assigned to the project. Likewise I can use $estimates->description inside a foreach( $project->estimates), but I cannot use $estimates->type->name, since the Types table is not directly related to the Projects table.

Thus; Nesting the contains parameter. This lets me join the Types table to the Estimates table – which lets me get the $estimates->type->name object.

Simple as that, but obviously really hard to find out (or I simply just suck at Googling).

Some of the google queries I tried before finding this out were:

cakephp show related field
cakephp get joined fileld 
cakephp include related model

Raspberry Pi and Homebridge

1. Getting Linux installed on your Raspberry Pi

Download Raspbian:

Write image to SD card (instructions:

Put the SD card in the Raspberry Pi and let it boot up

Figure out what IP adress the Raspberry Pi has been assigned, either through your router or by running arp -a to show all devices on your network.

Connect to the RPi via SSH:
Default username: pi
Default password: raspberry

ssh pi@<ip address>

“The authenticity of host ‘ (’ can’t be established.” – type yes and press enter. This is just a warning to make sure you’re not connecting to an unknown server.

Congratulations, you are now logged in to your RPi.

Now expand the file system to fill the entire SD card (and make space for our Homebridge installation). Run raspi-config and select the option to expand the file system.


Then reboot the system and reconnect once it’s back online:

sudo reboot
- wait for half a minute or so -
ssh pi@<ip address>

2 Installing NodeJS

First, find the latest (or preferred) version of NodeJS on their website (or in ). If you have a Raspberry Pi 1 you will need the file with armv6l in it’s name, and the Pi 2 will need the armv7l one.

At the time of writing this, the LTS version is 4.2.6 – the version I’ll be using for this guide.

Once you have the url to the correct file, run the following command to download it to you Pi:


Unpack the file with the following command:

tar -xvf node-v4.2.6-linux-armv7l.tar.gz

Enter the node-v4.2.6-linux-armv7l directory and copy it’s contents to

cd node-v4.2.6-linux-armv7l
/usr/local: sudo cp -R * /usr/local/

That’s it, NodeJS is now installed on your Raspberry Pi. You can verifiy this by running node -v, which should return v4.2.6 (or whatever version you downloaded):

node -v

3. Installing Homebridge

Before installing Homebridge, you need to make sure you have libavahi-compat-libdnssd-dev installed:

sudo apt-get install libavahi-compat-libdnssd-dev.

Also, we will need Git:

sudo apt-get install git

We can now install homebridge:

npm install -g homebridge

You should now be able to run homebridge, which will fail with a few errors.


4. Configuring Homebridge

First of all, we will need a configuration file. Create one using the following command:

nano ~/.homebridge/config.json

Paste this example content into the file, and change values accordingly:

  "bridge": {
    "name": "My Homebridge Name",
    "username": "CC:22:3D:E3:CE:30",
    "port": 51826,
    "pin": "123-45-678"
  "description": "My Homebridge description here",
  "accessories": [
  "platforms": [

Save the file and try starting Homebridge by running the command homebridge. It should start and your PIN-code should be visible. You should also be able to find your homebridge in the HomeKit app of choice (iDevices and Eve are fairly good, free apps).

5. Adding plugins

Now we’re ready to start adding plugins to get some actual functionality. Plugins are installed through npm:

npm install -g homebridge-http

When the installation has finished, edit the accessories-portion of your config.json to look like this (obviously with your own addresses):

"accessories": [
    "accessory": "Http",
    "name": "Doomsday device",
    "switchHandling": "yes",
    "http_method": "GET",
    "on_url": "http://whisper.address/command_on",
    "off_url": "http://whisper.address/command_off",
    "status_url": "http://whisper.address/status",
    "service": "Light",
    "brightnessHandling": "yes",
    "brightness_url": "http://http://whisper.address/brightness",
    "brightnesslvl_url": "http://whisper.address/brightlvl",
    "sendimmediately": "",
    "username" : "",
    "password" : ""

Save the file and run Homebridge again. You should now see it initialize your newly added accessory. Start your Homekit-app and try to add a new product – it should show up.

6. Starting Homebridge automatically at system start

First we need to create an init script, and for the sake of getting things done – copy this script to your clipboard:

Create and edit a new init script, and paste the contents on your clipboard in it:

sudo nano /etc/init.d/homebridge

Change the first lines of the files to this:

# Provides: homebridge
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.

cmd="DEBUG=* /usr/local/bin/homebridge"

Now change the permissions for the file and “install” it:

sudo chmod 755 /etc/init.d/homebridge
sudo update-rc.d homebridge defaults

Homebridge will now launch on reboot, and you can manually start it with this command:

sudo /etc/init.d/homebridge start

Run this command to verify that homebridge starts as expected, and to see any errors. The command will show the last 30 entries from the system log:

tail -n 30 /var/log/syslog

That’s it!

At the time of writing this guide there are issues with Hombridge failing within minutes or hours from startup, a restart of homebridge seems to solve this until a fix is issued:

sudo /etc/init.d/homebridge restart