【问题标题】:How to get last insert record - Yii 2如何获取最后一条插入记录 - Yii 2
【发布时间】:2017-01-04 06:15:12
【问题描述】:

我正在申请创建条形码 在我的应用程序中,我想创建 Barang 但在问题单中我希望从上一个问题单自动填写问题单 我该怎么做才能解决它?

这是我的看法

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model backend\models\Barang */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="barang-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'rm_code')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'nama_barang')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'berat')->textInput(['maxlength' => true]) ?>



    <?= $form->field($model, 'issue_slip')->textInput(['maxlength' => true]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

 <div class="row">
    <?= $form->field($model, 'berat')->textInput(['maxlength' => true]) ?>
    <?php echo  \Yii::$app->db->getLastInsertId('{{barang}}');?>

  </div>

【问题讨论】:

  • 两者是相同的模型属性吗?
  • @YasarArafath 是的,一样,我该怎么办?

标签: yii yii2 yii2-advanced-app yii-components yii2-basic-app


【解决方案1】:

渲染前在控制器中

public function actionCreate()
{
    $model = new Barang();
    $modelForSlip = Barang::find()->orderBy(['id'=> SORT_DESC])->one();
    $model->issue_slip = $modelForSlip->issue_slip;

    if ($model->load(Yii::$app->request->post())){

       $model->waktu = date('Y-m-d h:m:s');
       $model->user = \Yii::$app->user->identity->username;
       $model->save();

        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
         //$model->issue_slip= \Yii::$app->db->getLastInsertId();
    }
}

【讨论】:

  • 立即查看答案
  • 检查 print_r($modelForSlip->attributes);
  • 我保存时得到 0
  • 检查 print_r($model->errors);在 $model->Save(); 之后
  • @yasarafarath OKE BRO!那是工作!谢谢你的帮助:)
【解决方案2】:

假设这是你的创建函数:

 public function actionCreate()
    {
        $model = new Modal();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
             return $this->redirect(['update', 'id' => $model->Id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

然后重定向到最后插入的新页面ID 现在让你像这样更新函数

 public function actionUpdate($id)
        {
            $model2 = Model::findOne($id);
            $model = new Modal();
 $model->issue_slip = $model2['issue_slip'];




            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(['view', 'id' => $model->Id]);
            } else {
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        }

YII2 中,您可以在save() 函数之后轻松获取最后插入的记录

喜欢

 $model->save();
echo $model->Id 

或任何你想得到的列 而不是issue_slip,你可以使用任何你想要的列

【讨论】:

  • 我什么也没得到
  • 当您保存任何模型时,在调用 save 函数之后,您可以从上次保存的数据中获取任何内容,例如 $model-&gt;save(); echo $model-&gt;Id 或您想要使用的 db 中的任何列名,而不是 @ 987654329@你可以将它传递给任何函数,也可以使用任何方式
  • 每次创建新记录后,您都可以将其重定向到update 页面,然后根据需要使用最后插入的数据,请再次查看我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
  • 2012-01-23
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 2020-05-19
相关资源
最近更新 更多