Multiple models in form

Hi,

learning Yii2 I am trying to do the follwing:

There are 4 tabels in a database. 3 tables contain singular information:

customers

staff

accounts

The fifth table draws back on the information in the other 4 tables, e.g.

invoices

It relates to the other tables via id, which means in the table ‘invoices’ the id of the customer, the id of the staffer and the id of the account is saved.

I was very successful in generating up the CRUD-pages for the first three tables. Gii is wonderful, took me only about an hour to get it working. Amazing!

BUT: generating CRUD for the ‘invoices’ table is harder. Naturally I want the create-Form to have dropdown-Fields for the customer, the staffer and the account. These should be generated out of the respective tables.

I read through the Yii2 Definitve Guide, Chapter: Getting Data for multiple models, and tried to adjust the information to my needs.

My ‘InvoicesController.php’ looks like that




<?php


namespace app\controllers;


use Yii;

use app\models\Rechnungen;

use app\models\RechnungenSearch;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;

use app\models\Customers;

use app\models\Staff;

use app\models\Accounts;


/**

 * InvoicesController implements the CRUD actions for Invoices model.

 */

class InvoicesController extends Controller

{

  ...

  

    /**

     * Creates a new Invoices model.

     * If creation is successful, the browser will be redirected to the 'view' page.

     * @return mixed

     */

    public function actionCreate()

    {

        $model = new Invoices();

        

        // return all Customers and order them by their name

        $customers = Customers::find()

        	->orderBy('name')

        	->all();

        	

        // return all staffers and order them by their name

        $staff = Staff::find()

        	->orderBy('name')

        	->all();

        

        // return all accounts and order them by their name

        $accounts = Accounts::find()

        	->orderBy('name')

        	->all();

        

        if ($model->load(Yii::$app->request->post())){

        	$model->created_at = time();

        	$model->updated_at = time();

        	if($model->save()) {

            return $this->redirect(['view', 'id' => $model->r_id]);

          }

        }

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

        	'model' => $model,

        	'customers' => $customers,

        	'staff' => $staff,

        	'accounts' => $accounts,

        ]);

    }

  ...

}



After a little debugging ;) that seemed to throw no errors anymore.

My create-Form ‘views/invoices/_form.php’ looks like that, and I started with trying to create a Dropdown-List for customers only, to get the principle right:




<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;


/* @var $this yii\web\View */

/* @var $model app\models\Invoices */

/* @var $customers app\models\Customers */

/* @var $staff app\models\Staff */

/* @var $accounts app\models\Accounts */

/* @var $form yii\widgets\ActiveForm */

?>


<div class="rechnungen-form">


    <?php 

    	$DDcustomers = array();

    	if($customers){

    		foreach($customers as $c){$DDcustomers[$c->id] = $c->Name;}

    	}

        $form = ActiveForm::begin(); 

    ?>


    <?= $form->field($model, 'customer')->dropDownList($DDcustomers,

    	['prompt'=>'- Choose a customer -']) ?>


    ... other form fields ...




That code results in an error




PHP Notice – yii\base\ErrorException 

Undefined variable: customers



As I just started to learn yii a couple days ago, I guess I am still confused on how the MVC-model works and therefor put the directives into the wrong place… Thanks for any help.

hbergman

You should take a look at this lesson




https://www.youtube.com/watch?v=Kg17wiJfGA8&index=10&list=PLRd0zhQj3CBmusDbBzFgg3H20VxLx2mkF



hope it helps.

You should create a Form Model that will encapsulate all that models and use it in your controller.