Reputation: 365
I have three tables, you can see in this picture bellow. And then I've created form skpaa and the data values in form will save to table skpaa and update one the field in the other table. I have the problem to form SKPAA, when user save to table skpaa I want the data value in table anak_asuh is like status changed based on kode anak asuh in form SKPAA. I've implemented with the following refrences use method Executing Non-SELECT Queries from doc yii2. But the result is strange when I save, the all data values in field status is changed. I want the data value in field status changed based on form SKPAA. Please tell me how to solutions.. Thanks
Controller:
public function actionCreate()
{
// Skpaa
$skpaa = new Skpaa();
if (!$skpaa) {
throw new NotFoundHttpException("The skpaa was not found.");
}
// Display datetime in form and save to table
$skpaa->tgl_skpaa = date('Y-m-d');
// Dispaly kode pengurus in form and save to table
$skpaa->kode_pengurus = \Yii::$app->user->identity->kode_pengurus;
// Ska
$ska = new Ska();
if (!$ska) {
throw new NotFoundHttpException("The ska was not found.");
}
// Calon Keluarga Asuh
$calonKeluarga = new CalonKeluargaAsuh();
if (!$calonKeluarga) {
throw new NotFoundHttpException("The calon keluarga asuh was not found.");
}
// Anak asuh
$anak_asuh = new AnakAsuh();
if (!$anak_asuh) {
throw new NotFoundHttpException("The anak asuh was not found.");
}
$anak_asuh->kode_anak_asuh = Yii::$app->db->createCommand()->update('anak_asuh', ['status' => 'Tetap'])->execute();
// Calon anak
$calon = new Calon();
if (!$calon) {
throw new NotFoundHttpException("The Calon was not found.");
}
// Calon anak
$pengurus = new Pengurus();
if (!$pengurus) {
throw new NotFoundHttpException("The Pengurus was not found.");
}
if ($skpaa->load(Yii::$app->request->post()) && $anak_asuh->load(Yii::$app->request->post()) && $skpaa->save()) {
Yii::$app->session->setFlash('success', 'Data berhasil disimpan!');
return $this->redirect(['index']);
return $this->refresh();
}
return $this->render('create', [
'skpaa' => $skpaa,
'ska' => $ska,
'calonKeluarga' => $calonKeluarga,
'anak_asuh' => $anak_asuh,
'calon' => $calon,
'pengurus' => $pengurus,
]);
}
View :
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\widget\Pjax;
use yii\helpers\Url;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use kartik\widgets\Select2;
use yii\web\JsExpression;
use app\models\Skpaa;
use app\models\Ska;
use app\models\CalonKeluargaAsuh;
use app\models\AnakAsuh;
use app\models\Calon;
use app\models\User;
use app\models\Pengurus;
use kartik\widgets\DatePicker;
$this->title = 'Surat Keterangan Penyerahan Anak Asuh';
?>
<h1 align="center">Surat Keterangan Penyerahan Anak Asuh</h1>
<?php
echo " ";
echo " ";
?>
<?php $form = ActiveForm::begin([
'layout' => 'horizontal',
'fieldConfig' => [
'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
'horizontalCssClasses' => [
'label' => 'col-sm-4',
'offset' => 'col-sm-offset-4',
'wrapper' => 'col-sm-8',
'error' => '',
'hint' => '',
'button' => 'col-sm-4'
],
],
]);
?>
<div class="panel panel-default">
<div class="panel-heading"><b><h4 align="center">Form Tanda Terima</h4></b></div>
<?php if (Yii::$app->session->hasFlash('success')): ?>
<div class="alert alert-success alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
<h4><i class="icon fa fa-check"></i>Informasi!</h4>
<?= Yii::$app->session->getFlash('success') ?>
</div>
<?php endif; ?>
<div class="panel-body">
<table class="table table-striped">
<?php echo $form->field($skpaa, 'no_skpaa')->textInput(['style'=>'width:350px', 'readOnly' => true]);
?>
<?= $form->field($skpaa, 'tgl_skpaa')->widget(DatePicker::classname(), [
'options' => [
'id' => 'tgl_skpaa',
'style' => 'width:350px',
'disabled' => true
],
'type' => DatePicker::TYPE_INPUT,
'language' => 'id',
'pluginOptions' => [
'autoclose'=>true,
'format' => 'yyyy-mm-dd',
]
]);
?>
<?php
$dataSka = ArrayHelper::map(Ska::find()->all(), 'no_ska', 'no_ska');
?>
<?php
echo $form->field($skpaa, 'no_ska')->widget(Select2::classname(), [
'data' => $dataSka,
'language' => 'id',
'options' => [
'placeholder' => 'Pilih',
'id' => 'no_ska',
],
'pluginOptions' => [
'allowClear' => true,
'width' => '350px',
],
])->label('Nomor Surat Ketersediaan');
?>
<?php
echo $form->field($calon, 'nama_calon')->textInput(['style'=>'width:350px', 'disabled' => true]);
?>
<?php
echo $form->field($anak_asuh, 'kode_anak_asuh')->textInput(['style'=>'width:350px']);
?>
<?php
echo $form->field($calonKeluarga, 'nama')->textInput(['style'=>'width:350px', 'disabled' => true]);
?>
<?php
echo $form->field($calonKeluarga, 'jenis_kelamin')->textInput(['style'=>'width:350px', 'disabled' => true]);
?>
<?php
echo $form->field($calonKeluarga, 'alamat')->textInput(['style'=>'width:350px', 'disabled' => true]);
?>
<?php
echo $form->field($calonKeluarga, 'pekerjaan')->textInput(['style'=>'width:350px', 'disabled' => true]);
?>
<?php $kode_pengurus = \Yii::$app->user->identity->kode_pengurus ?>
<?php
echo $form->field($skpaa, 'kode_pengurus')->textInput(['style'=>'width:350px', 'value' => $kode_pengurus, 'disabled' => true]);
?>
<?php $nama_pengurus = \Yii::$app->user->identity->nama_pengurus ?>
<?php
echo $form->field($pengurus, 'nama_pengurus')->textInput(['style'=>'width:350px', 'value' => $nama_pengurus, 'disabled' => true]);
?>
<?php $alamat = \Yii::$app->user->identity->alamat ?>
<?php
echo $form->field($pengurus, 'alamat')->textInput(['style'=>'width:350px', 'value' => $alamat, 'disabled' => true]);
?>
<?php $pekerjaan = \Yii::$app->user->identity->pekerjaan ?>
<?php
echo $form->field($pengurus, 'pekerjaan')->textInput(['style'=>'width:350px', 'value' => $pekerjaan, 'disabled' => true]);
?>
<?php $jenis_kelamin = \Yii::$app->user->identity->jenis_kelamin ?>
<?php
echo $form->field($pengurus, 'jenis_kelamin')->textInput(['style'=>'width:350px', 'value' => $jenis_kelamin, 'disabled' => true]);
?>
<div class="form-group">
<div class="col-sm-offset-4">
<?= Html::submitButton('Simpan', ['class' => 'btn btn-primary']) ?>
<?php
echo " ";
echo " ";
echo Html::a('Keluar', ['/'], ['class' => 'btn btn-success']);
?>
</div>
</div>
</table>
</div>
</div> <!-- /panel -->
</div> <!-- /container -->
<?php ActiveForm::end();?>
Model:
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
use app\models\Skpaa;
use app\models\Pengurus;
use app\models\User;
use app\models\Ska;
class Skpaa extends ActiveRecord
{
public static function tableName()
{
return '{{%skpaa}}';
}
public function rules()
{
return [
[['no_ska', 'kode_pengurus'], 'required'],
[['tgl_skpaa'], 'safe'],
];
}
public function actributeLabels()
{
return [
[
'no_skpaa' => 'Nomor Surat',
'tgl_skpaa' => 'Tanggal Surat',
'kode_pengurus' => 'Kode Pengurus',
'no_ska' => 'Nomor Surat Ketersediaan',
],
];
}
// Autonumber for no_skpaa
public function behaviors()
{
return [
[
'class' => 'mdm\autonumber\Behavior',
'attribute' => 'no_skpaa', // required
//'group' => $this->id_branch, // optional
'value' => 'SKPAA'.'?' , // format auto number. '?' will be replaced with generated number
'digit' => 2 // optional, default to null.
],
];
}
public function getPengurus()
{
return $this->hasOne(Pengurus::className(), ['kode_pengurus' => 'no_skpaa']);
}
public function getSka()
{
return $this->hasOne(Ska::className(), ['no_ska' => 'no_skpaa']);
}
}
Upvotes: 1
Views: 1215
Reputation: 133360
In your command
Yii::$app->db->createCommand()->update('anak_asuh', ['status' => 'Tetap'])->execute();
you have not set any condtion so the updated is executed on all the rows
You should assigne some condition for filter the rows for update eg: assuming that you want update only the row with id = $ id you could use:
Yii::$app->db->createCommand()->update('anak_asuh', ['status' => 'Tetap'],
'your_id = ' . $id )->execute();
Looking at your conde I think that you have place the update in the wrong place try move this code
$kode_anak = $anak_asuh->kode_anak_asuh;
Yii::$app->db->createCommand()->
update('anak_asuh', ['status' => 'Tetap'],
"kode_anak_asuh = '" . $kode_anak . "'")->execute();
inside the code where you load and save the created models
if ($skpaa->load(Yii::$app->request->post()) && $anak_asuh->load(Yii::$app->request->post()) && $skpaa->save()) {
$kode_anak = $anak_asuh->kode_anak_asuh;
Yii::$app->db->createCommand()->
update('anak_asuh', ['status' => 'Tetap'],
"kode_anak_asuh = '" . $kode_anak . "'" )->execute();
Yii::$app->session->setFlash('success', 'Data berhasil disimpan!');
return $this->redirect(['index']);
return $this->refresh();
}
Upvotes: 1
Reputation: 365
I adding the code in actionCreate with following answers scaisEdge.
public function actionCreate()
{
// Skpaa
$skpaa = new Skpaa();
if (!$skpaa) {
throw new NotFoundHttpException("The skpaa was not found.");
}
// Display datetime in form and save to table
$skpaa->tgl_skpaa = date('Y-m-d');
// Dispaly kode pengurus in form and save to table
$skpaa->kode_pengurus = \Yii::$app->user->identity->kode_pengurus;
// Ska
$ska = new Ska();
if (!$ska) {
throw new NotFoundHttpException("The ska was not found.");
}
// Calon Keluarga Asuh
$calonKeluarga = new CalonKeluargaAsuh();
if (!$calonKeluarga) {
throw new NotFoundHttpException("The calon keluarga asuh was not found.");
}
// Anak asuh
$anak_asuh = new AnakAsuh();
if (!$anak_asuh) {
throw new NotFoundHttpException("The anak asuh was not found.");
}
$kode_anak = $anak_asuh->kode_anak_asuh;
Yii::$app->db->createCommand()->update('anak_asuh', ['status' => 'Tetap'], 'kode_anak_asuh = ' . $kode_anak )->execute();
// Calon anak
$calon = new Calon();
if (!$calon) {
throw new NotFoundHttpException("The Calon was not found.");
}
// Calon anak
$pengurus = new Pengurus();
if (!$pengurus) {
throw new NotFoundHttpException("The Pengurus was not found.");
}
if ($skpaa->load(Yii::$app->request->post()) && $anak_asuh->load(Yii::$app->request->post()) && $skpaa->save()) {
Yii::$app->session->setFlash('success', 'Data berhasil disimpan!');
return $this->redirect(['index']);
return $this->refresh();
}
return $this->render('create', [
'skpaa' => $skpaa,
'ska' => $ska,
'calonKeluarga' => $calonKeluarga,
'anak_asuh' => $anak_asuh,
'calon' => $calon,
'pengurus' => $pengurus,
]);
}
Upvotes: 0