public function actionUpdateEmailNotification($id) { $user = $this->_loadUser($id); $newValues = array(); if (!empty($_POST['value'])) { $newValues = (array) $_POST['value']; } $oldValues = UserNotification::model()->findAll('user_id=:user_id', array(':user_id' => $user->id)); if (!empty($oldValues)) { foreach ($oldValues as $key => $value) { if (false !== ($key_ar = array_search($value->notification_id, $newValues))) { // get key of $newValues array if value = id $value->by_email = true; $value->save(false); unset($newValues[$key_ar]); } else { $value->by_email = false; $value->save(false); } } } if (!empty($newValues)) { foreach ($newValues as $value) { $userNotification = new UserNotification(); $userNotification->user_id = $user->id; $userNotification->notification_id = (int) $value; $userNotification->by_email = true; if (!$userNotification->save()) { header('HTTP 400 Bad Request', true, 400); echo CJSON::encode($userNotification->errors); Yii::app()->end(); } } } echo '[]'; }