public function actionUpdate($id) { $scorecard = $this->findScorecard($id); if (in_array($scorecard->registration->competition->status, [Competition::STATUS_COMPLETED, Competition::STATUS_CLOSED])) { Yii::$app->session->setFlash('warning', Yii::t('igolf', 'Scorecard already published cannot be edited.')); return $this->render('view', ['scorecard' => $scorecard]); } if (isset($_POST['Score'])) { $count = 0; foreach (Yii::$app->request->post('Score') as $k => $dataToLoad) { $pk = explode('_', $k); if ($model = Score::findOne(['scorecard_id' => $pk[0], 'hole_id' => $pk[1]])) { $ret = $model->setAttributes($dataToLoad); if ($model->save()) { $count++; } } } if ($count > 0) { $scorecard->updateScorecard(); Yii::$app->session->setFlash('success', Yii::t('igolf', 'Scorecard updated.')); } } else { $scorecard->makeScores(); } return $this->render('update', ['scorecard' => $scorecard, 'dataProvider' => new ActiveDataProvider(['query' => $scorecard->getScores()])]); }
public function actionUpdateScores($id) { $practice = $this->findModel($id); $scorecard = $practice->getScorecard(true); if (isset($_POST['Score'])) { $count = 0; foreach (Yii::$app->request->post('Score') as $k => $dataToLoad) { $pk = explode('_', $k); if ($model = Score::findOne(['scorecard_id' => $pk[0], 'hole_id' => $pk[1]])) { $ret = $model->setAttributes($dataToLoad); if ($model->save()) { $count++; } } } if ($count > 0) { Yii::$app->session->setFlash('success', Yii::t('igolf', 'Scorecard updated.')); } } $scorecard->makeScores(); return $this->render('update', ['model' => $practice]); }
/** * Finds the Score model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Score the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Score::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }