使用 form create yii2 时更新另一个 table 中的字段有问题
Update field in the other table when using form create yii2 is something wrong
我有三个table,你可以在下面这张图片中看到。然后我创建了表单 skpaa,表单中的数据值将保存到 table skpaa 并更新另一个 table 中的字段。我在形成 SKPAA 时遇到问题,当用户保存到 table skpaa 我想要 table anak_asuh[ 中的数据值=33=] 就像 status 根据 kode anak asuh 以 SKPAA 形式更改。我已经使用 doc yii2 中的以下引用使用方法 Executing Non-SELECT Queries 实现了。但是当我保存时结果很奇怪,字段status中的所有数据值都被改变了。 我希望根据表单 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']);
}
}
在你的指挥下
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();
看着你的conde
我认为您将更新放在错误的位置尝试移动此代码
$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();
}
我在 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,
]);
}
我有三个table,你可以在下面这张图片中看到。然后我创建了表单 skpaa,表单中的数据值将保存到 table skpaa 并更新另一个 table 中的字段。我在形成 SKPAA 时遇到问题,当用户保存到 table skpaa 我想要 table anak_asuh[ 中的数据值=33=] 就像 status 根据 kode anak asuh 以 SKPAA 形式更改。我已经使用 doc yii2 中的以下引用使用方法 Executing Non-SELECT Queries 实现了。但是当我保存时结果很奇怪,字段status中的所有数据值都被改变了。 我希望根据表单 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']);
}
}
在你的指挥下
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();
看着你的conde 我认为您将更新放在错误的位置尝试移动此代码
$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();
}
我在 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,
]);
}