/** * Remove user from dept * @param type $person_id * @return type */ public function removeUserFromDept($person_id) { $now = new CDbExpression('NOW()'); $creator = Yii::app() instanceof CWebApplication ? Yii::app()->user->id : NULL; return Yii::app()->db->createCommand()->update(DeptUser::model()->tableName(), array('has_left' => 1, 'date_left' => $now, 'last_modified' => $now, 'last_modified_by' => $creator), '`person_id`=:t1 AND `has_left`=:t2', array(':t1' => $person_id, ':t2' => 0)); }
/** * Update user dept */ public function updateDeptUser() { if (!empty($this->dept_id)) { if (!DeptUser::model()->exists('`person_id`=:t1 AND `dept_id`=:t2 AND `has_left`=:t3', array(':t1' => $this->id, ':t2' => $this->dept_id, ':t3' => 0))) { DeptUser::model()->removeUserFromDept($this->id); DeptUser::model()->addUserToDept(array('person_id' => $this->id, 'dept_id' => $this->dept_id, 'date_created' => new CDbExpression('NOW()'), 'created_by' => Yii::app() instanceof CWebApplication ? Yii::app()->user->id : NULL)); } } else { DeptUser::model()->removeUserFromDept($this->id); } }