/** * @return bool * @throws \Exception */ public function createFamily() { if (!$this->validate()) { return false; } $spouses = Human::find()->where(['id' => [$this->firstSpouseId, $this->secondSpouseId], 'id_descendant_family' => null])->all(); if (!is_array($spouses) || count($spouses) !== 2) { return false; } $transaction = Yii::$app->db->beginTransaction(); try { $family = new Family(); $family->setAttribute('name', $spouses[0]->surname . '-' . $spouses[1]->surname); if (!$family->save()) { throw new Exception('Family creation error'); } foreach ($spouses as $human) { $human->setAttribute('id_descendant_family', $family->id); if (!$human->update(true, ['id_descendant_family'])) { throw new Exception('Spouses updating error'); } } } catch (Exception $e) { $transaction->rollBack(); Yii::$app->session->setFlash('danger', 'Произошла ошибка'); return false; } $transaction->commit(); $this->family = $family; return true; }
/** * Creates data provider instance with search query applied * @param array $params * @return ActiveDataProvider */ public function search($params) { $query = Human::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_ASC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['ilike', 'name', $this->name])->andFilterWhere(['ilike', 'surname', $this->surname]); return $dataProvider; }
/** * Finds the Human model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @param null|array $with * @return Human the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id, $with = null) { $query = Human::find()->where(['id' => $id]); if (is_array($with)) { $query->with($with); } if (($model = $query->one()) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }