Saving associated data: unwanted new join table entries

The three models involved in my problem are Resources, ResourcesRoles, and Roles.

Resources map to URLs, defined by paths like “/users/add”. Roles are defined by the website owner and each Resource is linked to each Role exactly once in the database using the join table ResourcesRoles. The join table also holds the boolean permission value at the intersection of every Resource/Role, so when I update a Resource in the db, or a Role, I want to modify the same entry in the join table, but instead I keep creating new entries in the join table when trying to update the Resources or Roles.

ResourcesTable model:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('resources');
    $this->displayField('name');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongsToMany('Roles', [
        'foreignKey' => 'resource_id',
        'targetForeignKey' => 'role_id',
        'joinTable' => 'resources_roles',
        'through' => 'resources_roles'
    ]);

    $this->hasMany('ResourcesRoles', [
        'foreignKey' => 'resource_id'
    ]);
}

RolesTable model:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('roles');
    $this->displayField('name');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->hasMany('Users', [
        'foreignKey' => 'role_id'
    ]);
    $this->belongsToMany('Menus', [
        'foreignKey' => 'role_id',
        'targetForeignKey' => 'menu_id',
        'joinTable' => 'menus_roles'
    ]);
    $this->belongsToMany('Resources', [
        'foreignKey' => 'role_id',
        'targetForeignKey' => 'resource_id',
        'joinTable' => 'resources_roles',
        'through' => 'resources_roles'
    ]);
    $this->hasMany('ResourcesRoles', [
        'foreignKey' => 'role_id'
    ]);
}

ResourcesRolesTable model:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('resources_roles');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongsTo('Resources', [
        'foreignKey' => 'resource_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Roles', [
        'foreignKey' => 'role_id',
        'joinType' => 'INNER'
    ]);
}

Here’s what my submitted data looks like when submitted to /resources/edit/28, for example:

[name] => Add Avatar
[path] => /avatars/add
[description] => Allow users to upload an image to the server
[resources_roles] => Array
    (
        [0] => Array
            (
                [id] => 162
                [resource_id] => 28
                [role_id] => 1
                [permission] => 1
            )

        [1] => Array
            (
                [id] => 163
                [resource_id] => 28
                [role_id] => 2
                [permission] => 1
            )

        [2] => Array
            (
                [id] => 164
                [resource_id] => 28
                [role_id] => 3
                [permission] => 1
            )

        [3] => Array
            (
                [id] => 165
                [resource_id] => 28
                [role_id] => 4
                [permission] => 1
            )

        [4] => Array
            (
                [id] => 166
                [resource_id] => 28
                [role_id] => 5
                [permission] => 1
            )

        [5] => Array
            (
                [id] => 270
                [resource_id] => 28
                [role_id] => 6
                [permission] => 0
            )
    )

I have included the IDs of the joint table entries that I want to update, but it doesn’t seem to matter. New entries are created in the join table, anyway.

I think I’ve solved the problem by adding the option saveStrategy => replace to the hasMany() association defined in the ResourcesTable model. I assume it will work for the RolesTable model the same.