如何将模型ID发送到ActiveFrom字段

时间:2017-06-09 15:37:57

标签: yii2

我有一个视图团队/视图,其中我有团队的详细信息(来自模型团队)。

视图/团队/ view.php:

<?php
    use yii\helpers\Html;
    use yii\widgets\DetailView;
    use yii\grid\GridView;
    use yii\data\ActiveDataProvider;
    use app\models\Pt;    

    /* @var $this yii\web\View */
    /* @var $model app\models\Team */

    $this->title = $model->name;
    $this->params['breadcrumbs'][] = ['label' => 'Teams', 'url' => ['index']];
    $this->params['breadcrumbs'][] = $this->title;
    ?>
    <div class="team-view">

        <h1><?= Html::encode($this->title) ?></h1>    
        <p>
            <?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
            <?= Html::a('Delete', ['delete', 'id' => $model->id], [
                'class' => 'btn btn-danger',
                'data' => [
                    'confirm' => 'Are you sure you want to delete this item?',
                    'method' => 'post',
                ],
            ]) ?> 
        </p>

        <?= DetailView::widget([
            'model' => $model,
            'attributes' => [              
                'tournament.name',
                'teamindex',
                'region.name',       
                'rank',
            ],
        ]) ?>

    <?= Html::a('add players to the team', ['pt/create'], ['class'=> 'btn btn-success']) ?>   
    </div>

最后一个链接是一个链接按钮,可以将玩家添加到团队中:<?= Html::a('add players to the team', ['pt/create'], ['class'=> 'btn btn-success']) ?>

在views / pt / create中我有一个带有字段的ActiveForm:team(下拉列表),player(下拉列表)。所以,如果我想向球队添加球员,我应该从一个looong下拉列表中选择球队的名字,然后选择一个球员(和一些球员选项......)。

这是views / pt / create.php:

    <?php

    use yii\helpers\Html;
    use yii\helpers\ArrayHelper;

    /* @var $this yii\web\View */
    /* @var $model app\models\Pt */

    $this->title = 'Add players to the team';
    $this->params['breadcrumbs'][] = ['label' => 'Players in teams', 'url' => ['index']];
    $this->params['breadcrumbs'][] = $this->title;
    ?>
    <div class="pt-create">

        <h1><?= Html::encode($this->title) ?></h1>    
        <?= $this->render('_form', [
            'model' => $model,
        ]) ?>  
    </div>

这是views / pt / _form.php:

    <?php

    use yii\helpers\Html;
    use yii\bootstrap\ActiveForm;
    use app\models\Player;
    use yii\helpers\ArrayHelper;


    /* @var $this yii\web\View */
    /* @var $model app\models\Pt */
    /* @var $form yii\widgets\ActiveForm */
    ?>

    <div class="pt-form">

        <?php $form = ActiveForm::begin(['id'=>'owner-form']); ?>

        <?= $form->field($model, 'team_id')->dropDownList($model->teamList) ?>

        <?= $form->field($model, 'player_id')
            ->dropDownList(ArrayHelper::map(Player::find()->joinWith('category')
                ->orderBy(['lastname'=>SORT_ASC])->all(), 'id', 'lastname'));
        ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Add' : 'Update', ['class' => $model
        ->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>
    <?php ActiveForm::end(); ?>
</div> 

我想这样:当我在http://localhost/myproject/web/team/100时,点击该按钮<?= Html::a('add players to the team', ['pt/create'], ['class'=> 'btn btn-success']) ?>

然后转到pt / create现场团队已经定义了team_id = 100的位置,所以我只需要输入播放器。

我该怎么做?我知道这很简单,但我还在学习。

PS:模型TeamPt有关系,

模型/ Team.php

public function getPt()
    {
        return $this->hasMany(Pt::className(), ['team_id' => 'id']);
    }

1 个答案:

答案 0 :(得分:0)

您应该创建一个适当的阳离子来创建已经分配了id_team的玩家(或者您可以修改原始创建)

    /**
   * Creates a new Palyer model assigning the id_team.
   * If creation is successful, the browser will be redirected to the 'view' page.
   * @return mixed
   */
  public function actionCreateForTeam(id_team)
  {
      $model = new Player();

      if ($model->load(Yii::$app->request->post()) && $model->save()) {
          return $this->redirect(['view', 'id' => $model->id]);
      } else {
          $model->id_team = $id_team
          return $this->render('create', [
              'model' => $model,
          ]);
      }
  }

然后发送正确的id_team

  Html::a('add players to the team', ['pt/create-for-teaam', 'id_team'=> $model->team_id], 
   ['class'=> 'btn btn-success']) ?>