public function addUserRequirement() { if (!$this->validate()) { return false; } $user = User::findOne($this->user_id); if (Requirement::findOne($this->requirement_id === null) || $user === null) { throw new NotFoundHttpException('The requested page does not exist.'); } foreach ($user->requirements as $current_req) { if ($current_req->id == $this->requirement_id) { return false; } } $req = new UserRequirement(); $req->user_id = $this->user_id; $req->requirement_id = $this->requirement_id; return $req->save(); }
public function importShifts($data) { //Data should be a 2D array of the following //[title, start_timestamp, length, min_participants, max_participants, requirement_name] $count = 0; foreach ($data as $row) { $timestamp = strtotime($row[1]); if (isset($row[5])) { $requirement = Requirement::findOne(['name' => $row[5]]); } else { $requirement = null; } $shift = new Shift(); $shift->title = $row[0]; $shift->start_time = $timestamp; $shift->team_id = $this->id; $shift->length = $row[2]; $shift->min_needed = !empty($row[3]) ? $row[3] : null; $shift->max_needed = !empty($row[4]) ? $row[4] : null; $shift->requirement_id = isset($requirement) ? $requirement->id : null; $shift->active = true; if ($shift->save()) { $count++; } } Yii::$app->session->addFlash('success', sprintf("Imported %d of %d shifts successfully", $count, count($data))); }
/** * Finds the Requirement model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Requirement the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Requirement::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }