/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = PersonsChildrenRecord::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['BirthYear' => SORT_DESC]]]); $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; } // grid filtering conditions $query->andFilterWhere(['ID' => $this->ID, 'PersonID' => $this->PersonID]); if (!empty($this->BirthYear)) { if (preg_match('/^(?:\\s*(<>|<=|>=|<|>|=)){1}(.*)$/', $this->BirthYear, $matches)) { $value = $matches[2]; $op = $matches[1]; } else { $value = $this->BirthYear; $op = '='; } if (!empty($value)) { $query->andWhere('`BirthYear`' . $op . $value); } } return $dataProvider; }
/** * @param $personId * @return array|string|Response * @throws NotFoundHttpException */ public function actionChildren($personId) { $model = $this->findModel($personId); $children = []; for ($i = 0; $i < 3; $i++) { $child = new PersonsChildrenRecord(); $child->PersonID = $model->ID; $children[] = $child; } if (($errors = $this->performAjaxValidationMultiple($children)) !== null) { return $errors; } if (Model::loadMultiple($children, Yii::$app->request->post())) { foreach ($children as $child) { /** @var PersonsChildrenRecord $child */ $child->create(); } return $this->redirect(['/accounts/charge-balance', 'id' => $model->account->id]); } else { return $this->render('children', ['model' => $model, 'children' => $children]); } }
/** * Finds the PersonsChildrenRecord model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return PersonsChildrenRecord the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = PersonsChildrenRecord::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Количество детей * @return int|string */ public function getChildrenCount() { return $this->hasMany(PersonsChildrenRecord::className(), ['PersonID' => 'ID'])->count(); }