【问题标题】:Update data in Kartik Detailview didn't workKartik Detailview 中的更新数据不起作用
【发布时间】:2017-12-26 02:09:10
【问题描述】:

我已经使用 Kartik Detail View 进行了详细展示。此小部件具有 编辑 内联功能,可通过单击右上角的铅笔图标按钮,如下所示

然后表格将可以像这样编辑: 我已经编辑了数据,然后单击“Floopy Disk Icon”按钮保存。

什么也没发生,我的数据还是一样,我的更新没有成功。

这是我在 view.php 中的代码

<?=
DetailView::widget([
    'model' => $model,
    'condensed' => true,
    'hover' => true,
    'mode' => DetailView::MODE_VIEW,
    'panel' => [
        'heading' => 'Data Details',
        'type' => DetailView::TYPE_INFO,
    ],
    'attributes' => [
        'product',
        'productId',
        'distDate',
        'created_at',
        'created_by',
        [
            'attribute' => 'is_exported',
            'value' => $model->isExported->name,
        ],
    ],
    'buttons1' => '{update}',
])
?>

为什么小部件没有保存编辑的数据?我的代码有什么问题?

更新:

这是我控制器中的操作:

public function actionUpdate($id) {
    $model = $this->findModel($id);

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

我用的是纯动作,Controller里面的代码我没有改。

这是我的模型

<?php

namespace app\modules\vatout\models;

use Yii;

class VatoutFakturOut extends \yii\db\ActiveRecord
{
/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'vatout_faktur_out';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['user_id', 'parent_id', 'product'], 'required'],
        [['user_id', 'parent_id', 'is_exported'], 'integer'],
        [['distDate', 'updated_at', 'created_at'], 'safe'],
        [['updated_by', 'created_by'], 'string', 'max' => 16],
        [['product'], 'string', 'max' => 128],
        ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'producrID' => 'Product ID',
        'user_id' => 'User ID',
        'parent_id' => 'Parent ID',
        'distDate' => 'Dist Date',
        'Product' => 'Product',
        'is_exported' => 'Is Exported',
    ];
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getIsExported()
{
    return $this->hasOne(VatoutStatus::className(), ['id' => 'is_exported']);
}
}

【问题讨论】:

  • 那么处理请求的控制器呢?
  • 您应该通知我有关更改的信息。我没有注意到这一点。 :)
  • 对不起,我忘了注意到你:(,我的错
  • 我没有错。你能提供你的模型吗?
  • 嗨@EdvinTenovimas,模型已添加。请检查一下。谢谢

标签: yii2 updates edit detailview kartik-v


【解决方案1】:

线$model = $this-&gt;findModel($id);:

您在 Controller 上使用 $this,因此它会尝试从数据库(不存在)中查找控制器的数据。此外,控制器中没有findModel() 这样的方法,这应该用于具有ActiveRecord 扩展名的模型。正确的用法是:

$model = VatoutFakturOut::findOne($id);

但是,这个小部件似乎没有传递任何$id(因此,actionUpdate($id) 中的参数$id 是无用的)。如何获得身份证?来自Yii::$app-&gt;request-&gt;post()['VatoutFakturOut']['productId']。但是应该检查它是否已定义,否则会收到警告:

$post = Yii::$app->request->post();

if (empty($post['VatoutFakturOut']['productId'])) {
    throw new NotFoundHttpException('Not found.');
}

第三件事是你让用户能够编辑 ID(即使它没有被覆盖)......这是你不应该做的事情。我会稍微更改视图文件的代码:

'attributes' => [
    'product',
    [
        'attribute' => 'productId', 
        'options' => ['readonly' => true],
    ],
    'distDate',
    'created_at',
    'created_by',
    [
        'attribute' => 'is_exported',
        'value' => $model->isExported->name,
    ],
],

假设productId 是主键。这将不允许用户编辑此字段,但仍会发送到控制器。

最终结果:

public function actionUpdate()
{
    $post = Yii::$app->request->post();

    if (empty($post['VatoutFakturOut']['id'])) {
        throw new NotFoundHttpException('VatoutFakturOut not found.');
    }

    $model = VatoutFakturOut::findOne($post['VatoutFakturOut']['id']);

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

【讨论】:

    猜你喜欢
    • 2017-05-17
    • 2020-10-23
    • 2018-04-26
    • 2017-05-21
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    相关资源
    最近更新 更多