Putting A Data Into A Different Model From A Another Model Form.

Hi there,

I am new to Yii and am stuck here. I have two models : Post and Tag,

What I want to do is put another TextArea in the Post Create Form which would get the Tags and store them in the Tag model.

This is my _form.php for the post create :


/* @var $this PostController */

/* @var $model Post */

/* @var $form CActiveForm */


<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(



)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>

	<div class="row">

		<?php echo $form->labelEx($model,'title'); ?>

		<?php echo $form->textField($model,'title',array('size'=>45,'maxlength'=>45)); ?>

		<?php echo $form->error($model,'title'); ?>


	<script src="<?php echo Yii::app()->baseUrl.'/ckeditor/ckeditor.js'; ?>"></script>

	<div class="row">

		<?php echo $form->labelEx($model,'post'); ?>

		<?php echo $form->textArea($model,'post',array('id'=>'editor1')); ?>

		<?php echo $form->error($model,'post'); ?>



	<div class="row">

		<?php echo $form->labelEx($model,'status'); ?>

		<?php echo $form->dropDownList($model,'status',$model->getStatusType()); ?>

		<?php echo $form->error($model,'status'); ?>



	<script type="text/javascript">

    CKEDITOR.replace( 'editor1' );



	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>


<?php $this->endWidget(); ?>

</div><!-- form -->

I tried adding this to the above code :

<div class="row">

		<?php echo $form->labelEx($model,'tags'); ?>

		<?php echo $form->textField($model,'tags',array('size'=>45,'maxlength'=>45)); ?>

		<?php echo $form->error($model,'tags'); ?>


Now I want to add another textArea to this that would get the tags and store them to the Tag model. I tried to change the actionCreate() method in my PostController but still I was getting a Post.tags is not defined error.

I even added this method in the Post model :

public function addTags($tags){

		$tags->id = $this->id;

		return $tags->save();


But still its giving me errors.

Hi Sankalp, welcome to the forum.

Please take a look at this wiki:

How to use a single form to collect data for two or more models?

Can you post your model ? I guess you forgot to defined tags varible in your model , also post your error message .

Thank you so so much for the link!! It really worked! :)