Yii2 基本显示从另一个模型创建到另一个模型的视图
Yii2 Basic display create from from another model to a view from another model
我创建了一个名为 Channel 的 CRUD 和一个 CRUD Post,所以我想将创建 Post 表单添加到 Channel 的 DetailView;例如,当用户在 Alpha 详细信息下查看 Channel Alpha 时,他有一个来自 Post 的表单以在该 Channel
中创建一个 Post
用户可以查看频道的详细信息,也可以将 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
将addpost
更改为ly_addPost
显示如下
public function actionView($id)
{
$ly_addPost = new Posts();
return $this->render('view', [
'model' => $this->findModel($id),
'ly_addPost' => $ly_addPost,
]);
}
只需在视图文件中将 $ly_addPost
更改为 $addpost
<div class="col-md-12">
<?= $this->render ('_form', [
'addpost' => $addpost,
])
?>
...
我创建了一个名为 Channel 的 CRUD 和一个 CRUD Post,所以我想将创建 Post 表单添加到 Channel 的 DetailView;例如,当用户在 Alpha 详细信息下查看 Channel Alpha 时,他有一个来自 Post 的表单以在该 Channel
中创建一个 Post用户可以查看频道的详细信息,也可以将 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
将addpost
更改为ly_addPost
显示如下
public function actionView($id)
{
$ly_addPost = new Posts();
return $this->render('view', [
'model' => $this->findModel($id),
'ly_addPost' => $ly_addPost,
]);
}
只需在视图文件中将 $ly_addPost
更改为 $addpost
<div class="col-md-12">
<?= $this->render ('_form', [
'addpost' => $addpost,
])
?>
...