This is the Model User
class User extends AppModel
{
public $name = 'User';
public $useTable = 'users';
public $displayField = 'name';
public $belongsTo = array(
'Role' => array(
'className' => 'Role',
'foreignKey' => 'role_id'
)
);
}
and this is the Model Role
class Role extends AppModel
{
public $name = 'Role';
public $useTable = 'roles';
public $displayField = 'name';
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'role_id',
'dependent' => false
)
);
}
however when trying to make the following request
$roles = $this->User->Role->find('list');
These are the tables referenced by Models
CREATE TABLE IF NOT EXISTS 'roles' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'name' varchar(100) NOT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS 'users' (
'id' int(10) NOT NULL AUTO_INCREMENT,
'email' varchar(50) DEFAULT NULL,
'password' varchar(50) DEFAULT NULL,
'name' varchar(50) DEFAULT NULL,
'activation_code' varchar(50) DEFAULT NULL,
'status' int(10) DEFAULT NULL,
'created' datetime DEFAULT NULL,
'modified' datetime DEFAULT NULL,
'role_id' int(10) DEFAULT NULL,
PRIMARY KEY ('id'),
KEY 'FK_users_roles' ('role_id'),
CONSTRAINT 'FK_users_roles' FOREIGN KEY ('role_id') REFERENCES 'roles' ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
It is reported that Role
is a null object, does anyone know what is wrong?