Rendering models inside another model

Hello,

I would appreciate if anyone can help me with problem I have related with nesting model inside another model. I model Offer and model Note. Multiple notes are related to a single offer, and my problem is how to render create/update view to add notes to an offer? Is there any solution using renderPartial method so I can use create part of Note controller?

Than you in advance.

If my understanding is correct, what you want is something like linking multiple comments to a single post in a blog. Is that it? If so, downloading and looking into the codes of Yii’s blog demo would help, I think. :)

You can also use:


$model->field

for single one value but also


foreach($model->notes as $key => $value) { ... }

for fetch all value of your "secondary" model

Yes, but my problem is that notes are added to offer at the same time as offer (not after like in blog example). I need offer id to save note. Also, multiple notes are related to single offer and I don’t know the number of notes the user will add.

I managed to pull something out with fixed number of comments. I am not satisfied with the solution, but maybe you’ll get clearer idea what I’m talking about.

OfferController.php:




	public function actionCreate()

	{

		$model=new Offer;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			if($model->save())

			{

				foreach($_POST['Note'] as $notee)

				{

					$note=new Note;

					$note->offer_id=$model->id;

					$note->type_id=$notee['type_id'];

					$note->content=$notee['content'];

					$note->save();

				}

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

			}

		}


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

			'model'=>$model,

		));

	}


	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			if($model->save())

			{

				foreach($_POST['Note'] as $notee)

				{

					$note=Note::model()->findByPk($notee['id']);

					$note->content=$notee['content'];

					$note->type_id=$notee['type_id'];

					$note->save();

				}

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

			}

		}


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

			'model'=>$model,

		));

	}



offer/_form.php:




	<?php if($model->isNewRecord): ?>

		<?php for($x=0; $x<7; $x++): ?>

			<div class="row" id="<?php echo $x; ?>">

				<?php echo CHtml::textArea('Note['.$x.'][content]'); ?>

				<?php echo CHtml::dropDownList('Note['.$x.'][type_id]','',Note::getTypeOptions()); ?>

			</div>

		<?php endfor; ?>

	<?php endif; ?>


	<?php foreach($model->notes as $note): ?>

	<div class="row" id="<?php echo $note->id; ?>">

		<?php echo $form->hiddenField($note,'id',array('name'=>'Note['.$note->id.'][id]')); ?>

		<?php echo $form->textArea($note,'content',array('name'=>'Note['.$note->id.'][content]')); ?>

		<?php echo $form->dropDownList($note,'type_id',$note->getTypeOptions(),array('name'=>'Note['.$note->id.'][type_id]')); ?>

	</div>

	<?php endforeach; ?>



Thank you! :)