D
D
Damir Shaniyazov2021-02-24 16:01:04
Yii
Damir Shaniyazov, 2021-02-24 16:01:04

How to use a view of one model in a view of another model?

Hello! How to use a view of one model in a view of another model?
Let's say I have a Project model and also a generated CRUD. There is also an Offer model (with CRUD) and it has project_id and user_id fields and so on. In the view of the Project model, all information is displayed, and at the end of the page there should be a form (_form.php) of the Offer model. I did it with the following code. But for some reason, instead of showing empty fields, I get an error

Call to a member function isAttributeRequired() on null
.
<?php
        if ($model->task_status == "Открыт" && !Yii::$app->getUser()->getIsGuest() && $model->customer_id != Yii::$app->getUser()->getId()) {
            $this->render('@app/views/offer/create.php', ['offer' => new \app\models\Offer()]);
        }
    ?>

I'm assuming it's because of the required attributes in the Offer DB table. Then I decided to change to another code. And it seems to work, but is it right ...
<?php
        if ($model->task_status == "Открыт" && !Yii::$app->getUser()->getIsGuest() && $model->customer_id != Yii::$app->getUser()->getId()) {
            $offer = new \app\models\Offer();
            $offer->id_project = $model->id;
            $offer->performer_id = Yii::$app->getUser()->getId();
            $offer->date = date('Y-m-d');

            echo Yii::$app->view->renderFile('@app/views/offer/_form.php', ['offer'=>$offer]);
        }
    ?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question