0
This and 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 and 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
)
);
}
but 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;
is informed that Role
is a null object, someone knows what is wrong?
cake considers the object '$this -> User -> Role' to be null, so I am including Role in the user control uses and using it as an incremented model, but this is not the right way :S
– user5020
user, in this case does not work, because
User
belongs toRole
and not the other way around. You need this find for what? If you were calling in this way, inside Rolescontroller it would work, but inside Userscontrollers it will not work, because each user only belongs to a single Role, would not have what to list in this case.– Marcelo Aymone