knowill
knowill

Reputation: 69

how can i get data from table with relation many to many in yii2?

I have three tables

----------
mysql> show columns from employee;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(30)      | NO   |     | NULL    |                |
| lastname  | varchar(30)      | YES  |     | NULL    |                |
| position  | tinyint(1)       | NO   |     | 0       |                |
| email     | varchar(50)      | NO   |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> show columns from groups;
+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name_of_group | varchar(50)      | NO   |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> show columns from groups_of_employee;                                                                 
+-------------+------------------+------+-----+---------+----------------+                                         
| Field       | Type             | Null | Key | Default | Extra          |                                         
+-------------+------------------+------+-----+---------+----------------+                                        
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |                                          
| employee_id | int(10) unsigned | NO   | MUL | NULL    |                |         
| group_id    | int(10) unsigned | NO   | MUL | NULL    |                |                                          
+-------------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

and some code

class employee
public function getGroupsOfEmployee()
{
    return $this->hasMany(GroupsOfEmployee::className(), ['id' => 
                                                        'group_id']);
}
/**
 * @return \yii\db\ActiveQuery
 */
public function getGroups()
{
    return $this->hasMany(Groups::className(), ['employee_id' => 'id'])
                                         ->via('groupsOfEmployee');
            //->viaTable('groups_of_employee', ['group_id' => 'id']);
}

class groups
public function getGroupsOfEmployee()
{
    return $this->hasMany(GroupsOfEmployee::className(),  
                                        ['employee_id'   => 'id']);
}

for example i get

    $model =  Employee::findOne(1);
    var_dump($model->getGroups());

but i don't see any way how to get name of grooup from table called groups

Upvotes: 2

Views: 65

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

If the relation is based on getGroup

you should use

    $model =  Employee::findOne(1);

    var_dump($model->groups);
    var_dump($model->groupOfEmployee);

and accessing to the value

    var_dump($model->groups->id);
    var_dump($model->groupOfEmployee->id);

Upvotes: 1

Related Questions