【问题标题】:Yii2 Basic display create from from another model to a view from another modelYii2 Basic display 从另一个模型创建到另一个模型的视图
【发布时间】:2017-12-22 08:03:24
【问题描述】:

我创建了一个名为 Channel 的 CRUD 和一个 CRUD Post,所以我想将 create Post 表单添加到 Channel 的 DetailView;例如,当用户在 Alpha 详细信息下查看 Channel Alpha 时,他有一个来自 Post 的表单来在该 Channel 内创建一个 Post

用户可以查看频道的详细信息,也可以将帖子添加到该频道

类似于:

在通道控制器中

public function actionView($id)
    {
        $ly_addPost = new Posts();
        return $this->render('view', [
            'model' => $this->findModel($id),
            'addpost' => $ly_addPost,
        ]);
    }

在频道视图中我确实将其编辑为:

//Yii2代码

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\models\Channel */

$this->title = $model->Channel_name;
$this->params['breadcrumbs'][] = ['label' => 'Channels', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="channel-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Update', ['update', 'id' => $model->Channel_id], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'id' => $model->Channel_id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>
    <div class="col-md-12">
        <?= $this->render ('_form', [
            'addpost' => $ly_addPost,
        ])
        ?>

        <div class="posts-form">

            <?php $form = ActiveForm::begin(); ?>

            <?= $form->field($model, 'Posts_title')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'Posts_text')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'Posts_file')->textInput(['maxlength' => true]) ?>

            <?php //= $form->field($model, 'Posts_crdate')->textInput() ?>

            <?= $form->field($model, 'Channel_id')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'Permissions_id')->textInput() ?>

            <?php //= $form->field($model, 'user_id')->textInput() ?>

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

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

        </div>
    </div>

</div>

但我得到错误:

PHP 通知 – yii\base\ErrorException

未定义变量:ly_addPost

【问题讨论】:

    标签: php yii2 yii2-basic-app


    【解决方案1】:

    addpost 更改为ly_addPost 如下所示

    public function actionView($id)
    {
        $ly_addPost = new Posts();
        return $this->render('view', [
            'model' => $this->findModel($id),
            'ly_addPost' => $ly_addPost,
        ]);
    }
    

    【讨论】:

      【解决方案2】:

      只需在视图文件中将$ly_addPost 更改为$addpost

      <div class="col-md-12">
              <?= $this->render ('_form', [
                  'addpost' => $addpost,
              ])
              ?>
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        • 2011-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多