Display children in a view

Hi,

What would be the best way to show children in a view?

Ive got one table: Users

id

name

lastName

image

reportsTo

reportsTo contains an id from Users table and i need to show all users that report to current user in the view (just below "reporters" field.

/protected/views/users/_view.php




<div class="view">


	<?php echo CHtml::image(Yii::app()->request->baseUrl.'/images/users/'.$data->image, 'user', array('style'=>"border: 1px solid #999; display: inline-block; float: right;")); ?>

	<h2><?php echo CHtml::link(CHtml::encode($data->name.' '.$data->lastName), array('update', 'id'=>$data->id)); ?></h2>


	<b><?php echo CHtml::encode($data->getAttributeLabel('reportsToId')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->reportsTo->name.' '.$data->reportsTo->lastName), array('view', 'id'=>$data->reportsTo->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('reporters')); ?>:</b>


</div>



Thank you in advance

I think you need a join on self relation to do this.

Just try to add one more relation to your model:


'reporters' => array(self::HAS_MANY, 'Users', 'reportsTo'),

and then $data->reporters will contain an array with all current user reporters.

Thank you so much yugene.

Now i’ll check how could I order by name field those reporters array.