public function actionDown($type, $id) { $ret_val = false; $existing = new Rating(); $existing->queryOptions['andWhere'] = ['user_id' => \Yii::$app->user->getId(), 'remote_type' => $type, 'remote_id' => $id]; switch ($existing->find()->where($existing->queryOptions['andWhere'])->exists()) { case false: $model = new Rating(['remote_type' => $type, 'remote_id' => $id]); $model->rating = 0; $model->save(); break; default: $existing = new Rating(); $existing->queryOptions['andWhere']['remote_type'] = $type; $existing->queryOptions['andWhere']['remote_id'] = $id; $count = $existing->getCount(); switch (1) { case $count > 0: $ret_val = true; break; } break; } $this->renderResponse($ret_val); }