Sei sulla pagina 1di 9

Tugas Pemogramman Framework

Buat lah database dengan yii , dengan pembagian tugas sebagai berikut :

Database Pegawai / Karyawan ( Reza Pahlevi Haikal / 1644190036)

Jawab :

Database nya sebagai berikut :

- Struktur Database Pegawai

Coding an :

- Codingan Model nya ( models / Pegawai.php )

<?php

namespace app\models;

use Yii;

/**
* This is the model class for table "pegawai".
*
* @property string $nip
* @property string $nama
* @property string $jenkel
* @property int $jabatan
* @property string $nohp
* @property string $alamat
*/
class Pegawai extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'pegawai';
}

/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['nip', 'nama', 'jenkel', 'jabatan', 'nohp', 'alamat'], 'required'],
[['jabatan'], 'string', 'max' => 20],
[['nip'], 'string', 'max' => 10],
[['nama', 'alamat'], 'string', 'max' => 50],
[['jenkel'], 'string', 'max' => 11],
[['nohp'], 'string', 'max' => 15],
[['nip'], 'unique'],
];
}

/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'nip' => 'Nip',
'nama' => 'Nama',
'jenkel' => 'Jenis Kelamin',
'jabatan' => 'Jabatan',
'nohp' => 'Nohp',
'alamat' => 'Alamat',
];
}

public function dataJenkel(){


return[
'Laki - Laki' => 'Laki - Laki',
'Perempuan' => 'Perempuan'
];
}

public function dataJabatan(){


return[
'Sales' => 'Sales',
'Admin' => 'Admin',
'Programmer' => 'Programmer',
'Manager' => 'Manager'
];
}
}
- Codingan Controllerya ( controllers / PegawaiController.php )

<?php

namespace app\controllers;

use Yii;
use app\models\Pegawai;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
* PegawaiController implements the CRUD actions for Pegawai model.
*/
class PegawaiController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}

/**
* Lists all Pegawai models.
* @return mixed
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Pegawai::find(),
]);

return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}

/**
* Displays a single Pegawai model.
* @param string $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}

/**
* Creates a new Pegawai model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Pegawai();

if ($model->load(Yii::$app->request->post()) && $model->save()) {


return $this->redirect(['view', 'id' => $model->nip]);
}

return $this->render('create', [
'model' => $model,
]);
}

/**
* Updates an existing Pegawai model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param string $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);

if ($model->load(Yii::$app->request->post()) && $model->save()) {


return $this->redirect(['view', 'id' => $model->nip]);
}

return $this->render('update', [
'model' => $model,
]);
}

/**
* Deletes an existing Pegawai model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param string $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}

/**
* Finds the Pegawai model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return Pegawai the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Pegawai::findOne($id)) !== null) {
return $model;
}

throw new NotFoundHttpException('The requested page does not exist.');


}
}

- Codingan View ( views / pegawai / )

# View untuk form pengisian ( _form.php )

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */


/* @var $model app\models\Pegawai */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="pegawai-form">

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'nip')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'nama')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'jenkel')->dropDownList($model->dataJenkel(),[


'class' => 'form-control','prompt' => '-PILIH JENIS KELAMIN-'
]) ?>

<?= $form->field($model, 'jabatan')->dropDownList($model->dataJabatan(),[


'class' => 'form-control','prompt' => '-PILIH JABATAN-'
]) ?>

<?= $form->field($model, 'nohp')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'alamat')->textarea(['rows' => 6]) ?>

<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>

<?php ActiveForm::end(); ?>

</div>

#View untuk create ( create.php )

<?php

use yii\helpers\Html;

/* @var $this yii\web\View */


/* @var $model app\models\Pegawai */

$this->title = 'Create Pegawai';


$this->params['breadcrumbs'][] = ['label' => 'Pegawais', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="pegawai-create">

<h1><?= Html::encode($this->title) ?></h1>

<?= $this->render('_form', [
'model' => $model,
]) ?>

</div>

#View untuk update (update.php)

<?php

use yii\helpers\Html;

/* @var $this yii\web\View */


/* @var $model app\models\Pegawai */

$this->title = 'Update Pegawai: ' . $model->nip;


$this->params['breadcrumbs'][] = ['label' => 'Pegawais', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->nip, 'url' => ['view', 'id' => $model->nip]];
$this->params['breadcrumbs'][] = 'Update';
?>
<div class="pegawai-update">

<h1><?= Html::encode($this->title) ?></h1>

<?= $this->render('_form', [
'model' => $model,
]) ?>

</div>
#View untuk detail data ( view.php)

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */


/* @var $model app\models\Pegawai */

$this->title = $model->nip;
$this->params['breadcrumbs'][] = ['label' => 'Pegawai / Karyawan', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="pegawai-view">

<h1><?= Html::encode($this->title) ?></h1>

<p>
<?= Html::a('Update', ['update', 'id' => $model->nip], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->nip], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>

<?= DetailView::widget([
'model' => $model,
'attributes' => [
'nip',
'nama',
'jenkel',
'jabatan',
'nohp',
'alamat',
],
]) ?>

</div>
# Screenshot Hasil Codingan / Tampilannya

- Halaman Index / Halaman Utama

- Halaman Created Data / Input Data


- Halaman Update Data

- Halaman View Data / Detail Data

Potrebbero piacerti anche