Yii Framework Forum: Yii2 Basic display create from from another model to a view from another model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 16-December 17

Posted 22 December 2017 - 03:15 AM

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
0

#2 User is offline   umneeq 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 171
  • Joined: 10-October 11
  • Location:Russia

Posted 22 December 2017 - 04:06 AM

<?= $this->render ('_form', [
    'addpost' => $addpost, // You haven't $ly_addPost in View. You have $addpost, because in Controller you render with 'addpost' array index,
])

0

#3 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,533
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 22 December 2017 - 07:15 AM

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,
        ])
        ?>

0

#4 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 16-December 17

Posted 22 December 2017 - 01:09 PM

View Postalrazi, on 22 December 2017 - 07:15 AM, said:

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
0

#5 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,533
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 23 December 2017 - 12:39 AM

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']
]

0

#6 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 16-December 17

Posted 23 December 2017 - 03:51 AM

View Postalrazi, on 23 December 2017 - 12:39 AM, said:

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users