听起来你需要一个 n:m 关系:所以一个员工可以分配到多个部门,一个部门有很多员工。您需要一个所谓的数据透视表,将员工连接到一个或多个部门,如下所示:
tbl_employee(id、姓名、年龄等) tbl_employee_department(employee_id、department_id) tbl_department(id、姓名等)
现在您的员工关系将如下所示:
public function relations()
{
return array(
'departments' => array(self::MANY_MANY, 'Department', 'tbl_employee_department(employee_id, department_id)'),
);
}
您的部门关系如下所示:
public function relations()
{
return array(
'employees' => array(self::MANY_MANY, 'Employee', 'tbl_employee_department(employee_id, department_id)'),
);
}
现在 $employee->departments 将返回一个部门模型数组。但是你想要的表单是一个部门ID数组,所以你必须在你的员工模型中这样做
public $departmentIds=array();
public function afterFind()
{
if(!empty($this->departments)) {
foreach($this->departments as $id=>$department)
$this->departmentIds[]=$department->id;
}
parent::afterFind();
}
public function afterSave()
{
$tx = Yii::app()->db->beginTransaction();
try {
//delete all connections from this employee to any department first
$command = Yii::app()->db->createCommand();
$command->delete('tbl_employee_department', 'employee_id=:id', array(':id'=>$this->id));
//now reconnect the employee to the departments in the departmentIds array
foreach($this->departmentsIds as $id) {
$command = Yii::app()->db->createCommand();
$command->insert('tbl_employee_department', array(
'employee_id' => $this->id,
'department_id'=> (int)$id
));
}
$tx->commit();
} catch (Exception $e) {
$tx->rollback();
throw new CException('Something bad happend. Cancelled transaction and rolled back data! '.$e->getMessage());
}
parent::afterSave();
}
只要您从数据库中获取员工模型(通过 Employee::model()->find() 等),此代码就会从数据库中获取部门并将其 id 存储在 $departmentIds 数组中。当您调用 $employee->save() 并且保存过程成功时,将调用 afterSave 方法。然后它将启动一个事务,删除该员工与其分配到的所有部门之间的所有连接,然后添加存储在 departmentIds 数组中的所有部门。
如果你想通过 $model->setAttributes() 设置这个字段,你必须把它放在你的员工规则中,声明它是安全的(所以 Yii 允许它被批量分配)
public function rules()
{
return array(
//...all the other rules...
array('departmentIds', 'safe'),
);
}
我知道,看起来很复杂,但我猜它应该给你一个想法。剩下的唯一问题是通过表单设置 departmentIds 数组的 id,但是您可以分解一个 texfield 字符串来执行此操作等。我猜这应该不难