Answer the question
In order to leave comments, you need to log in
How to write relations()?
Something I didn’t get enough sleep today, I can’t do the most banal thing.
mysql> show fields from users;
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| first_name | varchar(50) | NO | | NULL | |
| last_name | varchar(50) | NO | | NULL | |
| email | varchar(50) | NO | UNI | NULL | |
| role_id | int(10) unsigned | NO | MUL | NULL | |
| registered | int(10) unsigned | NO | | NULL | |
| country | int(10) unsigned | NO | MUL | NULL | |
| sex | enum('0','1') | NO | | NULL | |
| dob | int(10) unsigned | NO | | NULL | |
| phone | varchar(20) | NO | | NULL | |
| mobile | varchar(20) | NO | | NULL | |
| sport | int(10) unsigned | NO | MUL | NULL | |
| promoted | int(10) unsigned | YES | | NULL | |
| passhash | varchar(40) | NO | | NULL | |
| salt | varchar(10) | NO | | NULL | |
| status | bigint(20) unsigned | NO | | 0 | |
+------------+---------------------+------+-----+---------+----------------+
16 rows in set (0.02 sec)
mysql> show fields from user_roles;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| role | varchar(20) | NO | UNI | NULL | |
| mask | int(10) unsigned | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
Answer the question
In order to leave comments, you need to log in
Ah, I found it.
/**
* Get relations
*
* @return array
*/
public function relations()
{
return array(
'role' => array(self::HAS_ONE, 'UserRole', array('id' => 'role_id'))
);
}
I don't believe. The relationship is not one to one, but one to many. You have many users per role. BELONGES_TO must be specified.
//in Users
array(self::BELONGS_TO, 'UserRole', 'role_id'),
//in UserRole
array(self::HAS_MANY, 'Users', 'role_id'),
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question