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?
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.
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,
));
}