You are on page 1of 8

Andreastevenskarnyoto

TutorialYiiFramework
karnyoto@gmail.com,fb:karnyotoandre

Membuatformisiandenganmengambildatadaritabellainyangsebagaiforeignkey (YiiFramework)
untuk mengingat kode suatu record agar dapat dimasukkan dalam form yang memiliki hubungan dengan tabel lain sangat susah, dengan menggunakan metode ajax dalam yii framework kita dapat melakukanmengambilandatadaritabellaintanpamerefreshhalamanforminput. Scenario: Kita memiliki table testcontoh yang terhubung dengan beberapa tabel diantarnya tabel admin, fakultas dan mahasiswa, dalam tabel testcontoh terdiri dari field id, iduser, idfakultas dan nimmhs. Iduser terhunbung sebagai foreign key ke tabel admin, idfakultas terhubung sebagai foreign key ke tabel fakultasdannimmhsterhubungsebagaiforeignkeyketabelmahasiswa.

Relasiantartabel

Andreastevenskarnyoto

TutorialYiiFramework
karnyoto@gmail.com,fb:karnyotoandre Pengerjaan: 1. LakukanpembuatanMODELdanCRUDmenggunakanGIIuntuktabeltestcontoh. 2. Masuk pada /protected/controller/TestcontohController.php dan tambahkan perintah seperti dbawahini: Catatan:backgroundyangberwarnakuningadalahtambahan UntukactionCreate()
$modelAdmin = new Admin('search'); $modelAdmin->unsetAttributes(); // clear any default values if (isset($_GET['Admin'])) $modelAdmin->attributes = $_GET['Admin']; $modelFakultas = new Fakultas('search'); $modelFakultas->unsetAttributes(); // clear any default values if (isset($_GET['Fakultas'])) $modelFakultas->attributes = $_GET['Fakultas']; $modelMahasiswa = new Mahasiswa('search'); $modelMahasiswa->unsetAttributes(); // clear any default values if (isset($_GET['Mahasiswa'])) $modelMahasiswa->attributes = $_GET['Mahasiswa'];

$model = new Testcontoh; // Uncomment the following line if AJAX validation is needed $this->performAjaxValidation($model); if (isset($_POST['Testcontoh'])) { $model->attributes = $_POST['Testcontoh']; if ($model->save()) $this->redirect(array('view', 'id' => $model->id)); } $this->render('create', array( 'model' => $model, 'modelAdmin' => $modelAdmin, 'modelFakultas' => $modelFakultas, 'modelMahasiswa' => $modelMahasiswa, ));

UntukactionUpdate($id)
$modelAdmin = new Admin('search'); $modelAdmin->unsetAttributes(); // clear any default values if (isset($_GET['Admin'])) $modelAdmin->attributes = $_GET['Admin']; $modelFakultas = new Fakultas('search'); $modelFakultas->unsetAttributes(); // clear any default values if (isset($_GET['Fakultas'])) $modelFakultas->attributes = $_GET['Fakultas']; $modelMahasiswa = new Mahasiswa('search'); $modelMahasiswa->unsetAttributes(); // clear any default values if (isset($_GET['Mahasiswa'])) $modelMahasiswa->attributes = $_GET['Mahasiswa'];

Andreastevenskarnyoto

TutorialYiiFramework
karnyoto@gmail.com,fb:karnyotoandre
$model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Testcontoh'])) { $model->attributes = $_POST['Testcontoh']; if ($model->save()) $this->redirect(array('view', 'id' => $model->id)); } $this->render('update', array( 'model' => $model, 'modelAdmin' => $modelAdmin, 'modelFakultas' => $modelFakultas, 'modelMahasiswa' => $modelMahasiswa, )); 3. Editfile/protected/view/testcontoh/create.php create.php <?php echo $this->renderPartial('_form', array( 'model' => $model, 'modelAdmin' => $modelAdmin, 'modelFakultas' => $modelFakultas, 'modelMahasiswa' => $modelMahasiswa, ) ); ?>

4. Editfile/protected/view/testcontoh/update.php update.php
<?php echo $this->renderPartial('_form', array('model'=>$model, 'modelAdmin' => $modelAdmin, 'modelFakultas' => $modelFakultas, 'modelMahasiswa' => $modelMahasiswa, )); ?>

5. Editfile/protected/view/testcontoh/_form.php
<?php /* @var $this TestcontohController */ /* @var $model Testcontoh */ /* @var $form CActiveForm */ ?>

<div class="form"> <?php $form = $this->beginWidget('CActiveForm', array( 'id' => 'testcontoh-form', 'enableAjaxValidation' => false, )); ?>

Andreastevenskarnyoto

TutorialYiiFramework
karnyoto@gmail.com,fb:karnyotoandre
<p class="note">Fields with <span class="required">*</span> are required.</p> <?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model, 'iduser'); ?> <?php echo $form->hiddenField($model, 'iduser'); ?> <input type="text" id="user_name" name="user_name" value="<?php echo isset($model->iduser) ? Admin::model()->findByPk($model->iduser)->username : "" ?>" readonly > <input type="button" value="Get List User" name="get_user_btn" onclick="$('#mydialog').dialog('open'); return false;" > <?php echo $form->error($model, 'iduser'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'idfakultas'); ?> <?php echo $form->hiddenField($model, 'idfakultas'); ?> <input type="text" id="fakultas_name" name="fakultas_name" value="<?php echo isset($model->idfakultas) ? Fakultas::model()->findByPk($model->idfakultas)>NMFAKMSFAK : "" ?>" readonly > <input type="button" value="Get List Fakultas" name="get_fakultas_btn" onclick="$('#mydialo2').dialog('open'); return false;" > <?php echo $form->error($model, 'idfakultas'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'nimmhs'); ?> <?php echo $form->hiddenField($model, 'nimmhs'); ?> <input type="text" id="mahasiswa_name" name="mahasiswa_name" value="<?php echo isset($model->nimmhs) ? Mahasiswa::model()->findByPk($model->nimmhs)>NMMHSMSMHS : "" ?>" readonly > <input type="button" value="Get List Mahasiswa" name="get_mahasiswa_btn" onclick="$('#mydialo3').dialog('open'); return false;" > <?php echo $form->error($model, 'nimmhs'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form -->

<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array( 'id' => 'mydialog', // additional javascript options for the dialog plugin 'options' => array( 'title' => 'Daftar Pengguna', 'autoOpen' => false, ), )); $this->widget('zii.widgets.grid.CGridView', array( 'id' => 'user-grid', 'dataProvider' => $modelAdmin->search(), 'filter' => $modelAdmin,

Andreastevenskarnyoto

TutorialYiiFramework
karnyoto@gmail.com,fb:karnyotoandre
'columns' => array( 'username', 'namalengkap', array( 'header' => '', 'type' => 'raw', 'value' => 'CHtml::Button("+", array("name" => "get_link" , "id" => "get_link", "onClick" => "$(\"#mydialog\").dialog(\"close\");$(\"#Testcontoh_iduser \") .val(\"". $data->id."\");$(\"#user_name \").val(\"". $data>username."\");"))', ), ), )); $this->endWidget('zii.widgets.jui.CJuiDialog'); ?>

<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array( 'id' => 'mydialo2', // additional javascript options for the dialog plugin 'options' => array( 'title' => 'Daftar Fakultas', 'autoOpen' => false, ), )); $this->widget('zii.widgets.grid.CGridView', array( 'id' => 'fakultas-grid', 'dataProvider' => $modelFakultas->search(), 'filter' => $modelFakultas, 'columns' => array( 'KDFAKMSFAK', 'NMFAKMSFAK', array( 'header' => '', 'type' => 'raw', 'value' => 'CHtml::Button("+",array("name" =>"get_link3","id" "get_link3", "onClick" => "$(\"#mydialo2\").dialog(\"close\"); $(\"#Testcontoh_idfakultas\").val(\"".$data>KDFAKMSFAK."\"); $(\"#fakultas_name \").val(\"". $data->NMFAKMSFAK."\"); "))', ), ), )); $this->endWidget('zii.widgets.jui.CJuiDialog'); ?> <?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array( 'id' => 'mydialo3', // additional javascript options for the dialog plugin 'options' => array( 'title' => 'Daftar Mahasiswa', 'autoOpen' => false, 'width' => 600,

=>

Andreastevenskarnyoto

TutorialYiiFramework
karnyoto@gmail.com,fb:karnyotoandre
), )); $this->widget('zii.widgets.grid.CGridView', array( 'id' => 'mahasiswa-grid', 'dataProvider' => $modelMahasiswa->search(), 'filter' => $modelMahasiswa, 'columns' => array( 'NIMHSMSMHS', // nim mahasiswa 'NMMHSMSMHS', // nama mahasiswa 'TELEPON', // telepn array( 'header' => '', 'type' => 'raw', 'value' => 'CHtml::Button("+",array("name" =>"get_link4","id" => "get_link4", "onClick" => "$(\"#mydialo3\").dialog(\"close\"); $(\"#Testcontoh_nimmhs\").val(\"".$data->NIMHSMSMHS."\"); $(\"#mahasiswa_name \").val(\"". $data->NMMHSMSMHS."\"); "))', ), ), )); $this->endWidget('zii.widgets.jui.CJuiDialog'); ?>

6. Hasilnyaakansepertidibawahini

Andreastevenskarnyoto

TutorialYiiFramework
karnyoto@gmail.com,fb:karnyotoandre

Andreastevenskarnyoto

TutorialYiiFramework
karnyoto@gmail.com,fb:karnyotoandre