public function addRole($role, $expire_time = null) { if (!($user_id = $this->id())) { throw new \Exception('Cannot add role to unsaved user'); } if ($this->hasRole($role)) { return true; } $role = Role::findByName($role); if (!$role) { throw new \Exception('Invalid role name'); } $user_role = new User_Role(); $user_role->setProp(array('user_id' => $user_id, 'role_id' => $role->id(), 'expire_time' => $expire_time)); if ($saved = $user_role->save()) { $this->roles = null; } return $saved; }