Save model in a loop

Hi




$model=new Room;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Room']))

		{

			$model->attributes=$_POST['Room'];

			if($model->save()){

				$this->redirect(array('admin','id'=>$model->id));

			}

		}


		$this->render('create',array(

			'model'=>$model,

		));



I have this code and I want to save multiple models in a loop

Someone to help me please

Like this?

http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html

or this?

http://www.yiiframework.com/doc-2.0/guide-input-multiple-models.html

It is not working. This is the view code:


$floors = Floor::model()->findByAttributes(array('id' => $_REQUEST['id']));

    for ($i = 0; $i < $floors->no_of_rooms; $i++) {

Kindly help

A basic loop would be something like the following




public function save($id){

  $floors = Floor::find()->where(['id' => $id]);

  foreach($floors as $floor){ 

   $model=new Room;

   $model->floor= $floor->id;

   $model->save();

  }

}

If it’s tabular input you should check out some of the widgets that handle this process completely.