One or more model in one controller

I have one controller, two models, one view and two collections(Information and Address) in mongodb. I have created one form and want to store the information into two collections. How can i solve this. Can i use two models in one controller?

I have tried something, is it correct?




class UpdateController extends \yii\web\Controller

{

    public function actionIndex()

    {

		

        $model = new HomeAddress();

		$model1 = new Address();

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

            Yii::$app->session->setFlash('contactFormSubmitted');

			return $this->refresh();

			}else{

			return $this->render('index',['model' => $model,]);

		}

    }


}



Below i have attached screenshot of view form, in that left part(Information) details want to store Information collection and right part(Address) want to store Address collection. For this i have created also two models.

Hi,

Yes you can use one or more models in one controller.

Read:

http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models

This is for Yii 1.1 - but it should show you one principle how to do it.

And it should not be difficult to adapt this for Yii 2.

Regards

I have created like this,

cnotroller





class UpdateController extends \yii\web\Controller

{

    public function actionIndex()

    {

       $homeaddress = new HomeAddress();

       $address = new Address();

       if ($homeaddress->load(Yii::$app->request->post()) &&  $address->load(Yii::$app->request->post())){

			

        $toSave = $homeaddress->toSave();

	$toSave = $address->toSave();

	if($toSave){

	$homeaddress->save(false);

	$address->save(false);

				

     return $this->render('index',['homeaddress' => $homeaddress,'address'=>$address]);

}

		

    }


}




models are like this





class HomeAddress extends \yii\mongodb\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function collectionName()

    {

		

        return ['user_information', 'Information'];

    }


    /**

     * @inheritdoc

     */

    public function attributes()

    {

		

        return [

            '_id',

			'name',

			'mobile',

			'email',

        ];

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

		

        return [

            [['name','mobile','email'], 'safe']

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

		

        return [

            '_id' => 'ID',

			'name' => 'Name',

			'mobile' => 'Mobile',

			'email' => 'Email',

            

        ];

    }

	public function toSave()

	{

		

		if($this->validate()){

			$homeaddress = new HomeAddress();

			$homeaddress->name= $this->name;

			$homeaddress->mobile= $this->mobile;

			$homeaddress->email= $this->email;

			$homeaddress->save();

			return true;

		}else{

		return false;

		}

	

	}

	

}









class Address extends \yii\mongodb\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function collectionName()

    {

        return ['user_information', 'Address'];

    }


    /**

     * @inheritdoc

     */

    public function attributes()

    {

        return [

            '_id',

            'address1',

            'address2',

            'city',

            'state',

        ];

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['address1', 'address2', 'city', 'state'], 'safe']

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            '_id' => 'ID',

            'address1' => 'Address1',

            'address2' => 'Address2',

            'city' => 'City',

            'state' => 'State',

        ];

    }

	public function toSave()

	{

		if($this->validate()){

			$address = new Address();

			$address->address1= $this->address1;

			$address->address2= $this->address2;

			$address->city= $this->city;

			$address->state= $this->state;

			$address->save();

		      return true;

		}else{

		return false;

		}

	

	}

	

}




But after this when i run the controller action it shows blank on browser,It doesn’t going to models.

Hey,

Why exactyl do you have written this "toSave()" functions?

Keep it simple and stick to the example given.

It should look something like this:




public function actionCreate()

{

    $homeaddress = new HomeAddress();

    $address = new Address();

    

    // if something was send via post

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

        

        // load post data into models 

        $homeaddress->load(Yii::$app->request->post()); 

        $address->load(Yii::$app->request->post()); 

        

        // validate the models 

        $isValid = $homeaddress->validate(); 

        $isValid = $address->validate() && $isValid;

        

        // if both models are valid

        if($isValid){

        

            // save both models the "false" deactivates validation

            $homeaddress->save(false); 

            $address->save(false);

            

            // render the view which should come after successfull input

            return $this->render('view', ['homeaddress' => $homeaddress,'address'=>$address]);

               

        }

        

        // render the input form of the both models 

        return $this->render('myInputForm', ['homeaddress' => $homeaddress,'address'=>$address]);

    }

}



No extra functions needed.

Also:

Should the models somehow be related? …

If yes where / how is the relation defined?

Regards

Problem solved, data collected by form is stored in both collections, Thank You…