Yii2中hasMany关系的MySQL列定义 [英] MySQL Column definition for hasMany relationship in Yii2
问题描述
我有一个员工表,定义了他们的字母数字员工 ID(目前为 9 个字符,但可以增加到 15 个字符)作为键:
I have an employees table defined with their alphanumeric employee_id (currently 9-characters but can increase upto 15) as the key:
CREATE TABLE employee (
emp_id VARCHAR(15) NOT NULL PRIMARY KEY,
emp_name VARCHAR(255) NOT NULL,
...
);
现在,我必须创建一个 Group 实体,其中每个员工都可以属于多个组:
Now, I've to create a Group entity where each employee can be part of multiple groups:
CREATE TABLE group (
group_id VARCHAR(15) NOT NULL PRIMARY KEY,
group_name VARCHAR(255) NOT NULL,
employees ????, <--- how should this be defined?
FOREIGN KEY fk_emp(employees) REFERENCES employee(emp_id)
);
我可以使用 gii 或手动创建控制器和视图,没有问题.群组创建/更新表单将为员工提供多项选择.
I can create the controller and view for this using gii or manually, without an issue. The group creation/update form will have a multi-select for employees.
作为替代,Yii2 是否支持集合?
As an alternative, does Yii2 support sets?
推荐答案
像这样:
您通过第三个表将组表和员工表链接在一起.这允许您将员工链接到许多组,并且组也可以链接到许多员工.
You link the group and employee tables together via a third table. This allows you to link employees into many groups, and groups to link to many employees as well.
这篇关于Yii2中hasMany关系的MySQL列定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!