MultiModelForm with Many to Many relations

Hello all,

I’m trying to use the MultiModelForm to add images to an h otel ( can’t write this without a space because of the spamfilter). Because I have multiple tables that need to have images I use many to many relations. The problem I’m having that I don’t know how to fill the h otel_has_images table.

I added this into the h otel model:




'h otelsHasImages' => array(self::MANY_MANY, 'Images', 'h otels_has_images(images_id, h otels_id)'),



And in the images model:




'h otelsHasImages' => array(self::MANY_MANY, 'H otels', 'h otels_has_images(images_id, h otels_id)'),



My create function looks like:




public function actionCreate()

	{

		Yii::import('ext.multimodelform.MultiModelForm');


		$model=new H otels;

		$images=new Images;


		$validatedImages=array();




		if(isset($_POST['H otels']))

		{

			$file = 'uploads/'.$_POST['H otels']['name'].'.jpg';

			$model->attributes=$_POST['H otels'];


			$model->logo = CUploadedFile::getInstance($model,'logo');

			if(isset($model->logo) && !empty($model->logo)) {

				$model->logo->saveAs($file);

				$model->logo = $file;

			}


			if(MultiModelForm::validate($images,$validatedImages,$deleteItems) && $model->save()) {

			 	$masterValues = array('h otels_id'=>$model->id);

				if(MultiModelForm::save($images,$validatedImages,$deleteItems,$masterValues)) {

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

				}

			}

		}


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

			'model'=>$model,

			'images'=>$images,

       		'validatedImages' => $validatedImages,

		));

	}



When i save a h otel it is adding image records to the database, but it is not adding a record to the h otel_has_images table. How can i do this?

Sorry forgot an image of the database scheme…see the attachment

hi you can write a separate helper method or you can do it in the same actioN


foreach($_POST['images'] as $image ) {

  $model = new Model;

  $model->attributes = $image;

  $image->save();

}

this is not the only solution but its the easiest one