简体   繁体   中英

Yii2 Basic display create from from another model to a view from another model

I create a CRUD called Channel and a CRUD Post, so I want to add create Post form to DetailView of Channel; example when a user view Channel Alpha under the Alpha details he have a form from Post to create a Post inside that Channel

user can view the detail of a Channel and also can add Post to that Channel

something similar to :

in Channel controller

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

and in channel view I did edit it to :

//Yii2 code

<?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>

but I get error :

PHP Notice – yii\\base\\ErrorException

Undefined variable: ly_addPost

Change addpost to ly_addPost show below

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

Just Change $ly_addPost to $addpost in view file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM