コード例 #1
0
ファイル: FamilyForm.php プロジェクト: ivan2ko/familiesEditor
 /**
  * @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;
 }
コード例 #2
0
 /**
  * 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;
 }
コード例 #3
0
 /**
  * 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.');
     }
 }
コード例 #4
0
ファイル: _form.php プロジェクト: ivan2ko/familiesEditor
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Human;
/* @var $this yii\web\View */
/* @var $model app\models\FamilyForm */
/* @var $form yii\widgets\ActiveForm */
$unmarried = Human::getUnmarriedDropDown();
?>

<div class="family-form">

    <?php 
$form = ActiveForm::begin(['enableAjaxValidation' => true, 'enableClientValidation' => false]);
?>

    <?php 
echo $form->field($model, 'firstSpouseId')->dropDownList($unmarried);
?>

    <?php 
echo $form->field($model, 'secondSpouseId')->dropDownList($unmarried);
?>

    <div class="form-group">
        <?php 
echo Html::submitButton('Сохранить', ['class' => 'btn btn-success']);
?>
    </div>
コード例 #5
0
ファイル: Family.php プロジェクト: ivan2ko/familiesEditor
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getChildren()
 {
     return $this->hasMany(Human::className(), ['id_ancestry_family' => 'id']);
 }