简体   繁体   English

在Yii2中使用关系联接多个表

[英]JOIN multiple table using relations in Yii2

I am trying to list some data through Kartik GridView widget in yii2 by using relations. 我试图通过使用关系通过yii2Kartik GridView小部件列出一些数据。 I have these tables 我有这些桌子

staffs

CREATE TABLE IF NOT EXISTS `staffs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(250) CHARACTER SET utf8 DEFAULT NULL,
  `department_id` int(11) DEFAULT NULL,
  `designation_id` int(11) DEFAULT NULL,
  `username` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
  `emailid` varchar(250) CHARACTER SET utf8 DEFAULT NULL,
  `staffrights` tinyint(2) DEFAULT '0',
  `staffstatus` tinyint(2) DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
designations

CREATE TABLE IF NOT EXISTS `designations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `designname` varchar(150) NOT NULL,
  `designation_group_id` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;



designation_group 


CREATE TABLE IF NOT EXISTS `designation_group` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `group_name` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

designations table is related to designation_group by designations.designation_group_id . designations表通过designations.designation_group_iddesignation_group相关。 designations table will have one or more values seperated by comma, of designation_group.id . designations表将具有一个或多个用逗号分隔的值designation_group.id

designations table is related to staffs table by staffs.designation_id =designations.id . designations表通过staffs.designation_id =designations.id与职员表相关。 In Staffs Model I have added relations like this Staffs Model中,我添加了这样的关系

public function getDesignations() {
        return $this->hasOne( Designations::className(), ['id' => 'designation_id']);
    }

and is working perfect. 并且工作完美。 But the relation for designation_group I tried like this: 但是我尝试这样的用于designation_group的关系:

public function getDesgroupstaffs(){
        return $this->hasOne(Designations::className() , ['id' => 'id'])
                    ->from(Designationgroup::tableName() ) ;
}

But it doesnt give the expected result. 但是它并没有给出预期的结果。 How the designation_group table can be joined so that all the designation group associated with the staff can also be displayed ? 如何连接designation_group表,以便也可以显示与职员关联的所有指定组? I want to show like, the first column of grid view will be designations, while filter of the same column should be DesignationGroup.group_name. 我想显示,网格视图的第一列将是名称,而同一列的过滤器应是DesignationGroup.group_name。 SO if any group_name is selected , it will show data of staffs associated with that group name 因此,如果选择了任何group_name,它将显示与该组名称关联的人员数据

Well, from what I was able to extract from your question, you want to be able to get the group name for each of the staffs. 好吧,从我能够从您的问题中提取的信息中,您希望能够获得每个员工的组名。 The code below will help you accomplish that task. 下面的代码将帮助您完成该任务。

Inside the staff model, create a relationship as stated below or you can use an existing one which I am sure Yii would have automatically generated it for you 在人员模型中,创建如下所述的关系,或者您可以使用现有的关系,我相信Yii会自动为您生成关系

STAFF model STAFF模型

public function getDesignation()
{
   return $this->hasOne(Designation::className(),['designation_id'=>'id]);
}

Inside the designation model,create another relation that links the degination model with the designationGroup model, which would have been automatically created as well 在名称模型内,创建另一个关系,将关联模型与名称组模型链接起来,该模型也将自动创建

Designation MOdel 型号名称

public function getDesignationGroup()
{
   return $this->hasOne(DesignationGroup::className(),['id'=>'designation_group_id]);
}

Finally, on your gridview, you can use the code below to get the group name 最后,在您的gridview上,您可以使用以下代码获取组名

$model->destination->designationGroup->group_name

I hope this solves your problem. 我希望这能解决您的问题。 Though i have used it couple of times. 虽然我已经使用过几次。

If I understand you, it's looks like my problem a few days ago. 如果我理解您的话,那似乎是几天前的问题。 check this out 看一下这个

I have three tables (contact, contact_groups and contact_contact_groups) which has many-to-many relation. 我有三个表(contact,contact_groups和contact_contact_groups),它们具有多对多关系。 It means one contact has many groups, and one group has many contacts. 这意味着一个联系人有很多组,而一组有很多联系人。 So I create one table to make relations One-to-Many. 因此,我创建了一个表以建立一对多关系。

but the result will like this : 但结果将是这样的:

contact | contact groups
-------------------------
contact #1 | business1, business2, family
contact #2 | family
contact #3 | student, partner

staff model 员工模型

use ->with(['designationgroup']) 使用->with(['designationgroup'])

public function getDesignation()
{
   return $this->hasOne(Designation::className(),['designation_id'=>'id])->with(['designationgroup']);
}

designation model 指定模型

public function getDesignationgroup()
{
   return $this->hasOne(DesignationGroup::className(),['id'=>'designation_group_id]);
}

On GridView , render the value as GridView ,将值呈现为

$model->destination->designationgroup->group_name

Hope it helps. 希望能帮助到你。

I will suggest you to remove the column designation_group_id from designation table, because it contains multiples comma separated values. 我建议您从指定表中删除列designation_group_id ,因为它包含多个逗号分隔的值。 Create one more table named designation_group_assigned, which will have the following columns: Id , designation_id , designation_group_id 再创建一个名为designation_group_assigned的表,该表将包含以下列: Iddesignation_iddesignation_group_id

In this table, we can insert multiple rows if one designation belongs to multiple groups. 在此表中,如果一个名称属于多个组,我们可以插入多行。

And then you can use the yii relationships for one to many records. 然后,您可以将yii关系用于一对多记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM