Randy Hall
Randy Hall

Reputation: 8177

Cakephp hasManyThrough looking for wrong associated table

I have a hasManyThrough relationship with

class Permission extends AppModel {
    public $hasMany = array(
        'PermissionRole'
    );
}

class Role extends AppModel {
    public $hasMany = array(
        'PermissionRole'
    );
}

class PermissionRole extends AppModel {
    public $belongsTo = array(
        'Permission', 'Role'
    );
}

When trying to hit a simple view action on the Permission's controller, I'm getting this error:

"Error: Table permission_roles for model PermissionRole was not found in datasource default"

According to CakePHP conventions, permissions_roles is the correct table name, which is what I have created. Why am I seeing this message, and how do I correct the issue?

Upvotes: 0

Views: 69

Answers (1)

ndm
ndm

Reputation: 60503

What you are experiencing is the correct/expected behavior.

The conventions you are referring to only apply to join tables that are used in actual HABTM associations. In that case permissions_roles would be correct, which reflects the two models/tables that it connects.

However you are not using an actual HABTM association in the sense of using the HABTM functionality provided by the ORM (Model::$hasAndBelongsToMany), but you are manually creating a many-to-many association, using one-to-many and many-to-one associations, and so the default table name conventions apply, where the plural model name is being used, and that is permission_roles.

The solution is to simply rename your table.

Upvotes: 1

Related Questions