I would like to take the blog comment model and turn the form into a mulitimodel form but I have not been able to work this out. Would appreciate if anyone could point me in the right direction.
Update - Further Information:
To hopefully let you know what I am after. Looking at #2 here I have this set up and working just fine (this is using the Post controller and views to create new Comments). The goal now is to be able to use that same _form (/wwwroot/blog/protected/views/comment/_form.php) and capture data from 2 models at the same time (i.e. Comment & OtherModel) using that same Post controller & view.
I add the required form input from OtherModel (name & description, see below) onto the Comment model form the page will not render giving an Error 500
Taken the design below I want to add another table (OtherModel) off of comment with a FK in comment linking the tables.
Controller
public function actionView()
{
$post=$this->loadModel();
$comment=$this->newComment($post);
$this->render('view',array(
'model'=>$post,
'comment'=>$comment,
));
}
protected function newComment($post)
{
$comment=new Comment;
$otherModel=new OtherModel;
if(isset($_POST['Comment'], $_POST['OtherModel']))
{
$comment->attributes=$_POST['Comment'];
$otherModel->attributes=$_POST['OtherModel'];
if($post->addComment($comment))
{
if($comment->status==Comment::STATUS_PENDING)
Yii::app()->user->setFlash('commentSubmitted','Thank you...');
$this->refresh();
}
}
return $comment;
}
model
public function addComment($comment)
{
$comment->other_id=$otherModel->other_id;
$otherModel->save();
if(Yii::app()->params['commentNeedApproval'])
$comment->status=Comment::STATUS_PENDING;
else
$comment->status=Comment::STATUS_APPROVED;
$comment->post_id=$this->id;
return $comment->save();
}
render form through CJuiTabs
'Comment'=>$this->renderPartial('/comment/_form',array($model->$comment=>),true)
form
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'comment-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'author'); ?>
<?php echo $form->textField($model,'author',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'author'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'content'); ?>
<?php echo $form->textArea($model,'content',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'content'); ?>
</div>
// added otherModel as part of MMF
<div class="row">
<?php echo $form->labelEx($otherModel,'name'); ?>
<?php echo $form->textField($otherModel,'name',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($otherModel,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($otherModel,'description'); ?>
<?php echo $form->textArea($otherModel,'description',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($otherModel,'description'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Submit' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->