Yii Framework Forum: Putting A Data Into A Different Model From A Another Model Form. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Putting A Data Into A Different Model From A Another Model Form. How to make the form and the model and the controller? Rate Topic: -----

#1 User is offline   Sankalp Singha 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 13-April 13

Posted 19 May 2013 - 07:21 AM

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 :
<?php
/* @var $this PostController */
/* @var $model Post */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'post-form',
	'enableAjaxValidation'=>false,
)); ?>

	<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'); ?>
	</div>

	<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>

	
	<div class="row">
		<?php echo $form->labelEx($model,'status'); ?>
		<?php echo $form->dropDownList($model,'status',$model->getStatusType()); ?>
		<?php echo $form->error($model,'status'); ?>
	</div>

	

	<script type="text/javascript">
    CKEDITOR.replace( 'editor1' );
	</script>

	
	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
	</div>

<?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'); ?>
	</div>



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.
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 19 May 2013 - 07:56 AM

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?
1

#3 User is offline   samilo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 231
  • Joined: 04-October 12

Posted 19 May 2013 - 08:36 AM

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

#4 User is offline   Sankalp Singha 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 13-April 13

Posted 19 May 2013 - 12:49 PM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users