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'); }
/** * 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.'); } }
<?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>