M
M
Mikhail Osher2012-07-13 17:17:28
MySQL
Mikhail Osher, 2012-07-13 17:17:28

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)

How can I describe the relations method so that I can write $user->role->mask?
In general, the head does not want to work today =\

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2012-07-13
@miraage

Ah, I found it.

/**
     * Get relations
     *
     * @return array
     */
    public function relations()
    {
        return array(
            'role' => array(self::HAS_ONE, 'UserRole', array('id' => 'role_id'))
        );
    }

D
Denis Medved, 2012-07-13
@BuCeFaL

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'),

and back
//in UserRole
array(self::HAS_MANY, 'Users', 'role_id'),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question