Add row to related table?


#1

I’m currently trying to move my schema made in MySQL Workbench over to CakePHP Migrations.

Currently, I’m trying to add permissions to the permissions table which are related to the roles table.

In My Migration, I have written the following to create the role itself:

// Load the RolesTable from the registry
$rolesTable = TableRegistry::get('Roles');

// Add an Administrator role
$role = $rolesTable->newEntity();
$role->name = 'Administrator';
$rolesTable->save($role);

This works fine, so I now started working on adding the permissions (in a different migration to keep stuff a bit more organized):

// Load the RolesTable from the registry
$rolesTable = TableRegistry::get('Roles');

// Create permissions for the Administrator role
$permissions = $rolesTable->Permissions->newEntity();
$permissions->access_cms = 1;
$role = $rolesTable->findByName('Administrator')->first();
$role->permissions = $permissions;
$rolesTable->save($role);

This, however, leads into the error Could not save permissions, it cannot be traversed when trying to run the migrations.
What am I doing wrong and how can I correct it?


#2

turned out, all I had to do was change:

$role->permissions = $permissions;

with

$role->permissions = [$permissions];