Menampilkan CDetailView dan Daftar Rinciannya

Kadang kita ingin menampilkan data detail CDetailView dengan daftar rinciannya dalam satu view. Misalnya data Kelas dan daftar peserta yang ikut kelas tersebut.

Saya sudah membuat katalog tebing Indonesia utk kegiatan panjat tebing. Ada kawasan, dalam kawasan bisa terdapat lebih dari satu tebing, dan di suatu tebing bisa terdapat beberapa jalur pemanjatan.

Model yg terlibat adalah Kawasan.php, Tebing.php dan Jalur.php semua ada di protected/models hasil dari Gii.

Buat relasi di model Kawasan.php




	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

                  'tebing'=>array(self::HAS_MANY, 'Tebing', 'idkawasan'), //nama relasi=tebing

                  'jalur'=>array(self::HAS_MANY, 'Jalur', 'idkawasan'), //nama relasi=jalur

		);

	}



Buat relasi di model Tebing.php




	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

                 'kawasan' => array(self::BELONGS_TO, 'Kawasan', 'idkawasan'),

                 'jalur'=>array(self::HAS_MANY, 'Jalur', 'idtebing'),

		);

	}



Buat relasi di model Tebing.php




	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

                  'kawasan' => array(self::BELONGS_TO, 'Kawasan', 'idkawasan'),

                  'tebing' => array(self::BELONGS_TO, 'Tebing', 'idtebing'),

		);

	}




Perhatikan model Kawasan.php, dan nama relasi di model ini. Iya, nama relasi ini akan dipanggil di protected/views/kawasan/view.php





<div class="picture">

<?php

if(!empty($model->foto))

{

 echo CHtml::image(Yii::app()->request->baseUrl.'/../images/data/'.$model->foto,"foto",array("width"=>300, "align"=>left));  

}

else

{

 echo CHtml::image(Yii::app()->request->baseUrl.'/../images/rock1.jpg',"foto manjat",array("width"=>300, "align"=>left));  

}

?>

</div>







<div class="view">

<?php


   $this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'idkawasan',

		'nama',

		'kecamatan',

                 array(

                    'name'=>'city',

                    'value'=>$model->city->cityname,

                       ),

                 array(

                    'name'=>'state',

                    'value'=>$model->state->statename,

                       ),

                'info:html',

		'other',

		//'user',

		//'timestamp',

	),

)); ?>


<br />

<h2>Daftar Tebing </h2>

<ol>

<?php $items = $model->tebing; //memanggil nama relasi tebing dari model Kawasan

foreach ($items as $item) {

	//echo '<li>' . $item->namatradisional .' ('.$item->namapopuler. ')</li>';

	echo '<li>' . CHtml::link($item->concatened, array ('/tebing/view', 'id' => $item->idtebing)) . '</li>';

}

?>

</ol>

    

<br />


<h2>Daftar Jalur Pemanjatan </h2>

<ol>

<?php $items = $model->jalur; //memanggil nama relasi jalur di model Kawasan

foreach ($items as $item) {

	//echo '<li>' . $item->nama .' ('.$item->panjang.' m'. ')</li>';

	echo '<li>' . CHtml::link($item->nama, array ('/jalur/view', 'id' => $item->id)) . '</li>';

}

?>

</ol>




Contoh screenshoot tampilannya terlampir. Kalau mau lihat live Contoh. Mhn maaf, tampilan map pada contoh live tidak dibahas di sini.