【问题标题】:how can i get data from table with relation many to many in yii2?如何从 yii2 中与多对多关系的表中获取数据?
【发布时间】:2016-11-12 15:52:28
【问题描述】:

我有三张桌子

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

还有一些代码

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']);
}

例如我得到了

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

但我看不到如何从名为组的表中获取组的名称

【问题讨论】:

    标签: php activerecord yii yii2


    【解决方案1】:

    如果关系是基于getGroup的

    你应该使用

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

    并访问值

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 2022-12-20
      相关资源
      最近更新 更多