Yii two Models in one View

[size="3"]I have two tables 1.JUser ,2 Jprofile[/size]

[size="5"][u][b]

JUserController[/b][/u][/size]

public function actionView($id)

{


	$model =  $this->loadModel($id);


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


		'model_u'=>$model,


		'model_p'=>$model->jProfiles, // check relations in JUser


	));


}

[size="5"]View of JUser[/size]

<?php

/* @var $this JUserController */

/* @var $model_u JUser */

/* @var $model_p JProfile */

$this->breadcrumbs=array(

'Jusers'=&gt;array('index'),


&#036;model_u-&gt;userid,

);

$this->menu=array(

array('label'=&gt;'List JUser', 'url'=&gt;array('index')),


array('label'=&gt;'Create JUser', 'url'=&gt;array('create')),


array('label'=&gt;'Update JUser', 'url'=&gt;array('update', 'id'=&gt;&#036;model_u-&gt;userid)),


array('label'=&gt;'Delete JUser', 'url'=&gt;'#', 'linkOptions'=&gt;array('submit'=&gt;array('delete','id'=&gt;&#036;model_u-&gt;userid),'confirm'=&gt;'Are you sure you want to delete this item?')),


array('label'=&gt;'Manage JUser', 'url'=&gt;array('admin')),

);

?>

<h1>View JUser #<?php echo $model_u->userid; ?></h1>

<?php $this->widget(‘zii.widgets.CDetailView’, array(

'data'=&gt;&#036;model_u,


'attributes'=&gt;array(


	'userid',


	'username',


	'email',


),

)); ?>

<?php $this->widget(‘zii.widgets.CDetailView’, array(

'data'=&gt;&#036;model_p,


'attributes'=&gt;array(


	'profileid',


	'name',


	'surname',


),

)); ?>

[size="3"]Its give error: Property "JUser.jProfiles" is not defined.[/size]

Have you got the jProfiles key in the model relations array?

Looks like relation problem, probably something mess in model relations. Could U place here relations from JUser model?

1 Like

In JUser model

public function relations()

{

     return array(


	'jProfile' =&gt; array(self::HAS_MANY, 'Jprofile', 'Profileid'),  


     );

}

JProfile model

public function relations()

{

return array(


	'jUser' =&gt; array(self::HAS_ONE, 'Juser', 'Userid'), 


	);

}

You have got jProfile key there. Without ‘s’ at the end.