2 Models On One Form

Hi i have problem with using two module in one form

This is my problem

i try add input using club model

this is mysql Tables

tbl_contestant

-id

-name

-surname

-age

-club_id

tbl_club

-id

-name

this is my contestantController actionCreate




	public function actionCreate()

	{

		$m_contestant=new Contestant;

		$m_club=new Club;


		// Uncomment the following line if AJAX validation is needed

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


		if(isset($_POST['Contestant']) && isset($_POST['Club']))

		{

			$m_contestant->attributes=$_POST['Contestant'];

			$m_club->attributes=$_POST['Club'];

			if($m_contestant->save())

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

		}


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

			'm_contestant'=>$m_contestant,

			'club'=>$club,

		));

	}



this is Create view




<?php $this->renderPartial('_form', array('m_contestant'=>$m_contestant, 'club'=>$club)); ?>



this is _form view




		<?php echo $form->labelEx($club,'name'); ?>

		<?php echo $form->textField($club,name'); ?>

		<?php echo $form->error($club,'name'); ?>



i got CException "Property "Club.0" is not defined."

You got m_club in your code. From where you got $club suddenly during rendering in the controller ?

Controller


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

			'm_club'=>$m_club,

		));

Create view




<?php $this->renderPartial('_form', array('m_club'=>$m_club)); ?>



_form view


	<div class="row">

		<?php echo $form->labelEx($m_club,'name'); ?>

		<?php echo $form->textField($m_club,'name'); ?>

		<?php echo $form->error($m_club,'name'); ?>

</div>

return Property "Club.0" is not defined.

Is the relation properly set between two models ?

nope what relation i must use ?

It depends on your need. You can look through the below documentation :

http://www.yiiframework.com/wiki/181/relations-belongs_to-versus-has_one/

i need add club name to tbl_club and add id added club to tbl_contestant(club_id)

You are trying to use the “0” field but there isn’t any “0” field for your model class Club

yeap but i want use name




<?php echo $form->labelEx($m_club,'name'); ?>

<?php echo $form->textField($m_club,'name'); ?>

<?php echo $form->error($m_club,'name'); ?>



please explain me how can i display input from club

hello ameth69 you are using two models in your controller and creates two objects for each one and renders form contains for both models and when the form get submitted you are always trying to save contestant only so it makes the problem because club id in the contestant table refers to id of the club table. so if you save contestants first you will get these error only. so that club must be saved before contestant so that you can get club_id for contestant like this $m_contestant->club_id = $m_club->id; and save the $m_contestant->save()

for now i save nothing becouse i cant render textField