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 I add


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 :


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




<?= $this->render ('_form', [

    'addpost' => $addpost, // You haven't $ly_addPost in View. You have $addpost, because in Controller you render with 'addpost' array index,

])



to add to what umneeq said, good practice is to use a naming convention for example you could use the variable name $addPost everywhere like so


$addPost = new Posts();

        return $this->render('view', [

            'model' => $this->findModel($id),

                        'addpost' => $addPost,

        ]);




<?= $this->render ('_form', [

            'addpost' => $addPost,

        ])

        ?>

Thank you, now it’s working But it’s not create anything in the database

you need to make sure you have your addpost form pointing to the right action, by default it will submit to current action


[

'action' => ['/post/create']

]

Thank you it’s working