public function up() { $this->addColumn('{{%events_set}}', 'sport_id', $this->integer(11)); /* @var $defaultEventsSet EventsSet*/ $defaultEventsSet = EventsSet::findOne(1); $defaultEventsSet->sport_id = 1; $defaultEventsSet->save(); $this->alterColumn('{{%events_set}}', 'sport_id', 'INT(11) UNSIGNED NOT NULL AFTER `name`'); $this->addForeignKey('events_set_sport', '{{%events_set}}', 'sport_id', '{{%sport}}', 'id'); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = EventsSet::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query]); $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, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, '{{%sport}}.url_key' => $this->url_key]); $query->andFilterWhere(['like', '{{%events_set}}.name', $this->name]); $query->joinWith(['sport']); $query->joinWith(['events']); return $dataProvider; }
<div class="competition-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'name')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'sport_id')->dropDownList(ArrayHelper::map(Sport::find()->all(), 'id', 'name')); ?> <?php echo $form->field($model, 'events_set_id')->dropDownList(ArrayHelper::map(EventsSet::find()->all(), 'id', 'name')); ?> <?php echo $form->field($model, 'date_start')->widget(DatePicker::classname(), ['dateFormat' => 'yyyy-MM-dd']); ?> <?php echo $form->field($model, 'date_finish')->widget(DatePicker::classname(), ['dateFormat' => 'yyyy-MM-dd']); ?> <?php echo $form->field($model, 'location')->textInput(['maxlength' => true]); ?> <div class="form-group">
/** * Finds the EventsSet model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return EventsSet the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = EventsSet::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getEventsSets() { return $this->hasMany(EventsSet::className(), ['sport_id' => 'id'])->inverseOf('sport'); }
/** * @return \yii\db\ActiveQuery */ public function getEventsSet() { return $this->hasOne(EventsSet::className(), ['id' => 'events_set_id']); }
<?php use yii\helpers\Html; use yii\widgets\DetailView; /* @var $this yii\web\View */ /* @var $model common\models\Competition */ $this->title = $model->name; $this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Competitions'), 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; ?> <div class="competition-view"> <p> <?php echo Html::a(Yii::t('app', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']); ?> <?php echo Html::a(Yii::t('app', 'Delete'), ['delete', 'id' => $model->id], ['class' => 'btn btn-danger', 'data' => ['confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), 'method' => 'post']]); ?> </p> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['id', 'name', ['label' => $model->getAttributeLabel('sport_id'), 'value' => \common\models\Sport::findOne($model->sport_id)->name], ['label' => $model->getAttributeLabel('events_set_id'), 'value' => \common\models\EventsSet::findOne($model->events_set_id)->name], 'date_start', 'date_finish', 'location', 'created_at:datetime', 'updated_at:datetime']]); ?> </div>