我有三张桌子,你可以在这张图片中看到。然后我创建了表单skpaa,表单中的数据值将保存到表skpaa并更新另一个表中的字段。我有问题要形成 SKPAA ,当用户保存到表格草稿时我希望表格中的数据值 anak_asuh 就像状态更改基于< strong> kode anak asuh ,表格 SKPAA 。我使用doc yii2中的以下引用实现了使用方法Executing Non-SELECT Queries。但是当我保存时结果很奇怪,字段状态中的所有数据值都会更改。 我希望根据表单SKPAA 更改字段状态中的数据值。请告诉我如何解决方案..谢谢
控制器:
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,
]);
}
查看:
<?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();?>
型号:
<?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']);
}
}
答案 0 :(得分:1)
在你的命令中
Yii::$app->db->createCommand()->update('anak_asuh', ['status' => 'Tetap'])->execute();
您尚未设置任何条件,因此更新将在所有行上执行
您应该为过滤行更新某些条件,例如:假设您只想更新id = $ id的行,您可以使用:
Yii::$app->db->createCommand()->update('anak_asuh', ['status' => 'Tetap'],
'your_id = ' . $id )->execute();
看着你的康德 我认为您已将更新置于错误的位置尝试移动此代码
$kode_anak = $anak_asuh->kode_anak_asuh;
Yii::$app->db->createCommand()->
update('anak_asuh', ['status' => 'Tetap'],
"kode_anak_asuh = '" . $kode_anak . "'")->execute();
在您加载和保存创建的模型的代码中
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();
}
答案 1 :(得分:0)
我在actionCreate中添加了以下答案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,
]);
}