Hey there,
I’m a Yii beginner, have been using this for a week. So far so good, but now I’ve got relational db problem. I’ve read the documentation also, my code seems to be accurate, but my application just dies if I try to echo.
My code:
Under User model:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'role' => array(self::BELONGS_TO, 'Role', 'roleid'),
);
}
Under Role model:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'users' => array(self::HAS_MANY, 'User', 'roleid'),
);
}
Profile View:
<?php
$userdata = User::Model()->FindByPK($id);
print_r($userdata);
echo $userdata->username; ?>
<br>
<?php
echo $userdata->email;
?>
<br/>
<?php
echo $userdata->role;
?>
Probably some silly mistake. Maybe my relation isn’t correct because I added a column role(FK) later.
This is the SQL query:
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL,
`password` varchar(145) NOT NULL,
`hash` varchar(32) NOT NULL,
`email` varchar(64) NOT NULL,
`roleid` int(11) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT FOREIGN KEY (roleid) REFERENCES role(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Thank you for assistment