Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace backend\controllers;
- use Yii;
- use app\models\Karyawan;
- use app\models\Keluarga;
- use backend\models\Model;
- use backend\models\Pendidikan;
- use backend\models\KaryawanSearch;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use yii\helpers\ArrayHelper;
- /**
- * KaryawanController implements the CRUD actions for Karyawan model.
- */
- class KaryawanController extends Controller
- {
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['post'],
- ],
- ],
- ];
- }
- /**
- * Lists all Karyawan models.
- * @return mixed
- */
- public function actionIndex()
- {
- $searchModel = new KaryawanSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
- /**
- * Displays a single Karyawan model.
- * @param integer $id
- * @return mixed
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
- /**
- * Creates a new Karyawan model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return mixed
- */
- /**public function actionCreate()
- {
- $model = new Karyawan();
- $modelsKeluarga = [new Keluarga];
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->id_karyawan]);
- } else {
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- }**/
- public function actionCreate()
- {
- $model = new Karyawan;
- $modelsKeluarga = [new Keluarga];
- $modelsPendidikan = [new Pendidikan];
- if ($model->load(Yii::$app->request->post())) {
- $modelsKeluarga = Model::createMultiple(Keluarga::classname());
- Model::loadMultiple($modelsKeluarga, Yii::$app->request->post());
- $modelsPendidikan = Model::createMultiple(Pendidikan::classname());
- Model::loadMultiple($modelsPendidikan, Yii::$app->request->post());
- // ajax validation
- /* if (Yii::$app->request->isAjax) {
- Yii::$app->response->format = Response::FORMAT_JSON;
- return ArrayHelper::merge(
- ActiveForm::validateMultiple($modelsKeluarga),
- ActiveForm::validate($model)
- );
- }*/
- // validate all models
- $valid = $model->validate();
- $valid = Model::validateMultiple($modelsKeluarga) && $valid;
- $valid = Model::validateMultiple($modelsPendidikan) && $valid;
- if ($valid) {
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- if ($flag = $model->save(false)) {
- foreach ($modelsKeluarga as $modelKeluarga) {
- $modelKeluarga->nik = $model->nik;
- if (! ($flag = $modelKeluarga->save(false))) {
- $transaction->rollBack();
- break;
- }
- }
- foreach ($modelsPendidikan as $modelPendidikan) {
- $modelPendidikan->nik = $model->nik;
- if (! ($flag = $modelPendidikan->save(false))) {
- $transaction->rollBack();
- break;
- }
- }
- }
- if ($flag) {
- $transaction->commit();
- return $this->redirect(['view', 'id' => $model->id_karyawan]);
- }
- } catch (Exception $e) {
- $transaction->rollBack();
- }
- }
- }
- return $this->render('create', [
- 'model' => $model,
- 'modelsKeluarga' => (empty($modelsKeluarga)) ? [new Keluarga] : $modelsKeluarga,
- 'modelsPendidikan' => (empty($modelsPendidikan)) ? [new Pendidikan] : $modelsPendidikan
- ]);
- }
- /**
- * Updates an existing Karyawan model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param integer $id
- * @return mixed
- */
- /*public function actionUpdate($id)
- {
- $model = $this->findModel($id);
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->id_karyawan]);
- } else {
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- }*/
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
- //$modelsKeluarga = $model->nik;
- $modelsKeluarga = $model->nik0;
- if ($model->load(Yii::$app->request->post())) {
- $oldIDs = ArrayHelper::map($modelsKeluarga, 'nik', 'nik');
- $modelsKeluarga = Model::createMultiple(Keluarga::classname(), $modelsKeluarga);
- Model::loadMultiple($modelsKeluarga, Yii::$app->request->post());
- $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsKeluarga, 'nik', 'nik')));
- /*// ajax validation
- if (Yii::$app->request->isAjax) {
- Yii::$app->response->format = Response::FORMAT_JSON;
- return ArrayHelper::merge(
- ActiveForm::validateMultiple($modelsKeluarga),
- ActiveForm::validate($model)
- );
- }*/
- // validate all models
- $valid = $model->validate();
- $valid = Model::validateMultiple($modelsKeluarga) && $valid;
- if ($valid) {
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- if ($flag = $model->save(false)) {
- if (! empty($deletedIDs)) {
- Keluarga::deleteAll(['nik' => $deletedIDs]);
- }
- foreach ($modelsKeluarga as $modelKeluarga) {
- $modelKeluarga->nik = $model->nik;
- if (! ($flag = $modelKeluarga->save(false))) {
- $transaction->rollBack();
- break;
- }
- }
- }
- if ($flag) {
- $transaction->commit();
- return $this->redirect(['view', 'id' => $model->id_karyawan]);
- }
- } catch (Exception $e) {
- $transaction->rollBack();
- }
- }
- }
- return $this->render('update', [
- 'model' => $model,
- 'modelsKeluarga' => (empty($modelsKeluarga)) ? [new Keluarga] : $modelsKeluarga
- ]);
- }
- /**
- * Deletes an existing Karyawan model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param integer $id
- * @return mixed
- */
- public function actionDelete($id)
- {
- $this->findModel($id)->delete();
- return $this->redirect(['index']);
- }
- /**
- * Finds the Karyawan model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param integer $id
- * @return Karyawan the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = Karyawan::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement