Relational Active Record Question

Here i have a relational table on database 7037

Capture.PNG

I want to show all field from tbl_jadwal but the value for all columns are value from the relation table field. For example I want to show kode_hari from tbl_jadwal, but the value is nama_hari from tbl_hari. How to realize it using Relational Active Record?

Please Help.

Thanks.

declare relation in model class (tbl_jadwal)




public function relations() {

   return [

	'hari' => array(self::BELONGS_TO, 'tblHariModelClass', 'code_hari')

    ];	

}



after that when you find your model (jadwal), you can access data from relational table like :




$value = $model->hari->nama_hari;



1 Like

Thanks for the reply anyway, but can you tell me specific about the model? I want to encode the value with JSON and here the code I had tried




Public function actionMobile()

	{

            switch ($_GET['action']){

		

		Case 'view':

			$model = Hari::model()->findAll();                          

                        $value = $model->hari->nama_hari;

                        echo CJSON::encode($value);

			break; 			

            }

        }



And I got nothing with that

This is problem:

$model = Hari::model()->findAll();

findAll will return array of models;

Also relation is made in tbl_jadwal model class, and you are doing findAll on Hari model (suppose tbl_hari) and that model (Hari) don’t have hari relation.

if you do :




$model = Jadwal::model()->findByPk(1);

$value = $model->hari->nama_hari;



that will return ‘nama_hari’ value for wanted Jdwal model.

Or you can do:




$models = Jadwal::model()->findAll();

$hariValues = [];

foreach ($models as $model) {

    $hariValues[] = $model->hari->nama_hari;

}



in this case you will get all ‘nama_hari’ values for all Jadwal models, here you should probably want to index this $hariValues array with Jadwal.pk, like :




$hariValues[$model->primaryKey] = $model->hari->nama_hari;



1 Like

Ok its work brother, thanks a lot you are very helpful :D :D

But how if i want more columns from tbl_jadwal such as I want to get nama_hari , nama_kelas, nama_dosen through tbl_jadwal? 7038

Capture.PNG

Sorry, the problems above are solved, then I got a new problem, here is my code




$jadwals = Jadwal::model()->findAllBySql('SELECT kode_waktu, kode_matakuliah, NIP_dosen, id_ruangan FROM tbl_jadwal WHERE kode_kelas = :kelas AND kode_hari = :hari ORDER BY kode_waktu', Array(':kelas'=>$_GET['kode_kelas'], ':hari'=>$_GET['kode_hari']));

$jadwalValues = [];

foreach ($jadwals as $jadwal) {

    $jadwalValues = Array($jadwal->kodeWaktu->waktu, $jadwal->kodeMatakuliah->nama_matakuliah, $jadwal->nipDosen->nama_dosen, $jadwal->idRuangan->nama_ruangan); 

}

echo CJSON::encode($jadwalValues));

break;



Then i got the array like this:




["07.50-08.40","Rangkaian Logika","Malayusfi, BSEE., M.Eng.","A210"]



My apps cant read that array, but can read array like this:




[{"07.50-08.40","Rangkaian Logika","Malayusfi, BSEE., M.Eng.","A210"}]



How I can get the array where my apps can read?

I take back my questions above, cause I already solved the trouble.

Thanks again for helping me.