Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用表单create yii2时,另一个表中的Update字段出错_Php_Forms_Activerecord_Yii2_Active Form - Fatal编程技术网

Php 使用表单create yii2时,另一个表中的Update字段出错

Php 使用表单create yii2时,另一个表中的Update字段出错,php,forms,activerecord,yii2,active-form,Php,Forms,Activerecord,Yii2,Active Form,我有三张桌子,你可以在下面的图片中看到。然后我创建了表单skpaa,表单中的数据值将保存到表skpaa中,并更新另一个表中的一个字段。我无法形成SKPAA,当用户保存到表SKPAA时,我希望表anak_asoh中的数据值类似于基于kode-anak-asoh形式的SKPAA更改状态。我已经使用doc yii2中的以下参考使用方法实现了。但是当我保存时,结果很奇怪,字段status中的所有数据值都被更改了我希望根据表单SKPAA更改字段状态中的数据值。请告诉我如何解决问题。。谢谢 控制器: pub

我有三张桌子,你可以在下面的图片中看到。然后我创建了表单skpaa,表单中的数据值将保存到表skpaa中,并更新另一个表中的一个字段。我无法形成SKPAA,当用户保存到表SKPAA时,我希望表anak_asoh中的数据值类似于基于kode-anak-asoh形式的SKPAA更改状态。我已经使用doc yii2中的以下参考使用方法实现了。但是当我保存时,结果很奇怪,字段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 "&nbsp";
echo "&nbsp";
?>

<?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 "&nbsp";
echo "&nbsp";
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();
看看你的康德 我想你把更新放错地方了,试着移动这个代码

$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中添加了代码,并给出了以下答案

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,
    ]);
}

您可能应该在Skpaa模型中使用afterSave()方法:我已经尝试过了,结果是“Undefined variable:id”@Olik我的答案是一个示例,您应该使用正确的名称或表列,对于模型变量。。可能您需要的id是$kode\u anak\u????或者从您的模式中使用其他名称(您应该知道这一点)。。您可以使用$model->your\u code\u字段访问。。希望我的评论是正确的。。是的,你解释得很清楚。我试过了,但结果是错误的。“SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解在第1行“”附近使用的正确语法正在执行的SQL是:UPDATE
anak_asuh
SET
status
='Tetap',其中kode\u anak_asuh=”错误的原因是没有为您选择的变量分配任何值。请确保使用正确的变量,并且该变量包含正确的值。确保$kode_anak包含正确的值try var_dump($kode_anak)我已更新答案并添加引号..“kode_anak_asuh=”$科德阿纳克。"'"