Display 2 forms in one page

In my database I have a users table and a SalesReps table that have a one to one relationship.

When creating a new user I would like to display both users and SalesReps forms as one form but the data needs to go into their respective tables.

Is there a good approach to doing this?

Thanks,

Ben

I have the same problem.

The solution is here

http://www.yiiframew…oc/cookbook/19/

Thanks to Quiang

Awesome! Thanks for that post.

Dang, I felt like an idiot. I was thinking of asking about this as well. Thanks for the info!

I have the "Create" working fine but in the update I'm having some trouble with creating a join to display the values from each table.

I'm getting this error: Property "users.FirstName" is not defined. FirstName is part of the SalesReps table not users.

My relationship is between a users table and a salesreps table. The keys to join the 2 are user_ID. This is a one to one relationship. A user is a SalesRep.

In the users Model I have a relations method that looks like:

public function relations()

    {

    return array(

        'users'=>array(self::BELONGS_TO, 'users', 'user_ID'),

        'SalesReps'=>array(self::BELONGS_TO, 'SalesReps', 'user_ID'),

    );

  }

in the UsersController the actionUpdate() method looks like:

public function actionUpdate()


{


	$users=$this->loadusers();


	$salesreps=$this->loadusers();


	


    // validate BOTH $users and $salesreps

        $valid=$users->validate();

        $valid=$salesreps->validate() && $valid;

	if(isset($_POST['users'],$_POST['SalesReps']))


	{


		$users->attributes=$_POST['users'];


		$salesreps->attributes=$_POST['SalesReps'];


					


	    if ($valid)


		{


			$users->save();


			$salesreps->save();


			$this->redirect(array('show','id'=>$users->user_ID));


		}		


	 }


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


				  'users'=>$users,


				   'salesreps'=>$salesreps


				   ));


}

And the loadUsers() method looks like:

public function loadusers($id=null)

{

	if($this->_users===null)


	{


		if($id!==null || isset($_GET['id']))


			$this->_users=users::model()->with('SalesReps')->findbyPk($id!==null ? $id : $_GET['id']);


		if($this->_users===null)


			throw new CHttpException(500,'The requested users does not exist.');


	}


	return $this->_users;


}

Does anyone have an example of doing an update with this? I can't seem to get the relationships right between my users and SalesReps Models. The form always thinks that FirstName belongs to my users table.

Thanks,

Ben

What is the complete call stack? Which line does the error occur? Where is FirstName being accessed against users?

actually I just found that the query is trying to join the primary key in the users table(user_ID) with the primary key in the SalesReps table(salesRep_ID) instead of using the foreign key in the SalesReps table to do the join.

If I have my relations method using BELONGS_TO it tries to join on the wrong columng. If I change it to HAS_ONE the query looks correct but in my actionUpdate() method it's trying to assign FirstName to the users table.

Here is the call stack:

Stack Trace

#0 /Library/WebServer/Documents/thebigads/yii/framework/db/ar/CActiveRecord.php(390): CComponent->__get('FirstName')

#1 /Library/WebServer/Documents/thebigads/yii/framework/web/helpers/CHtml.php(1286): CActiveRecord->__get('FirstName')

#2 /Library/WebServer/Documents/thebigads/yii/framework/web/helpers/CHtml.php(881): CHtml::activeInputField('text', Object(users), 'FirstName', Array)

#3 /Library/WebServer/Documents/thebigads/protected/views/users/_form.php(65): CHtml::activeTextField(Object(users), 'FirstName', Array)

#4 /Library/WebServer/Documents/thebigads/yii/framework/web/CBaseController.php(119): require('/Library/WebSer…')

#5 /Library/WebServer/Documents/thebigads/yii/framework/web/CBaseController.php(88): CBaseController->renderInternal('/Library/WebSer…', Array, true)

#6 /Library/WebServer/Documents/thebigads/yii/framework/web/CController.php(652): CBaseController->renderFile('/Library/WebSer…', Array, true)

#7 /Library/WebServer/Documents/thebigads/protected/views/users/update.php(12): CController->renderPartial('_form', Array)

#8 /Library/WebServer/Documents/thebigads/yii/framework/web/CBaseController.php(119): require('/Library/WebSer…')

#9 /Library/WebServer/Documents/thebigads/yii/framework/web/CBaseController.php(88): CBaseController->renderInternal('/Library/WebSer…', Array, true)

#10 /Library/WebServer/Documents/thebigads/yii/framework/web/CController.php(652): CBaseController->renderFile('/Library/WebSer…', Array, true)

#11 /Library/WebServer/Documents/thebigads/yii/framework/web/CController.php(591): CController->renderPartial('update', Array, true)

#12 /Library/WebServer/Documents/thebigads/protected/controllers/UsersController.php(129): CController->render('update', Array)

#13 /Library/WebServer/Documents/thebigads/yii/framework/web/actions/CInlineAction.php(32): UsersController->actionUpdate()

#14 /Library/WebServer/Documents/thebigads/yii/framework/web/CController.php(265): CInlineAction->run()

#15 /Library/WebServer/Documents/thebigads/yii/framework/web/filters/CFilterChain.php(128): CController->runAction(Object(CInlineAction))

#16 /Library/WebServer/Documents/thebigads/yii/framework/web/filters/CFilter.php(41): CFilterChain->run()

#17 /Library/WebServer/Documents/thebigads/yii/framework/web/CController.php(885): CFilter->filter(Object(CFilterChain))

#18 /Library/WebServer/Documents/thebigads/yii/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))

#19 /Library/WebServer/Documents/thebigads/yii/framework/web/filters/CFilterChain.php(125): CInlineFilter->filter(Object(CFilterChain))

#20 /Library/WebServer/Documents/thebigads/yii/framework/web/CController.php(248): CFilterChain->run()

#21 /Library/WebServer/Documents/thebigads/yii/framework/web/CController.php(225): CController->runActionWithFilters(Object(CInlineAction), Array)

#22 /Library/WebServer/Documents/thebigads/yii/framework/web/CWebApplication.php(335): CController->run('update')

#23 /Library/WebServer/Documents/thebigads/yii/framework/web/CWebApplication.php(123): CWebApplication->runController('users/update')

#24 /Library/WebServer/Documents/thebigads/yii/framework/base/CApplication.php(170): CWebApplication->processRequest()

#25 /Library/WebServer/Documents/thebigads/index.php(11): CApplication->run()

#26 {main}

You ARE specifying user_ID as the foreign key in the relation…

I just got it working. In the actionDelete() method I needed to access my salesreps model by doing:

$salesreps=$users->salesreps;

I was trying to access it as:

$salesreps=$this->loadusers()

Thanks,

Ben