使用 yii x-editable 更新两个模型

Update two models using yii x-editable

因此,我正在使用此扩展程序:x-editable for yii

我目前正尝试在 update() 函数中更新两个模型。

我有两个型号:

Realisasi.php

RealisasiDeadline.php.

因此,当单元格在 table Realisasi.php 上更新时(t1701[= 列中的一个值42=] 在这种情况下),我希望函数更新 t1701 列中的相应值 table RealisasiDeadline,使用列no作为外键。

由于我在Google上没有找到任何例子,所以我自己编了一个:

public function actionSimpanEdit($kode) {
    Yii::import('editable.EditableSaver');
    $es = new EditableSaver($_GET['model']);  // 'modelName' is classname of model to be updated
    $es->update();

    $es2 = RealisasiDeadline::model()->findByPk($kode);//this is where I'm stuck
    $es2['t1701'] = '1991-11-19';//this too
    $es->update();//and this
}

这是view.php:

array(
        'name' => 't1701',
        'value' => 'CHtml::value($data,"hubtarget.t1701")=== "0"?"Target Nol":$data->t1701',
        'header' => 'Bkl Selatan',
        'class' => 'editable.EditableColumn',
        'editable' => array(
            'url' => $this->createUrl('simpanEdit', array('model' => 'Realisasi', 'kode'=>'$data->no')),
        )
    ),

我错过了什么?有可能吗?如果不行,还有其他解决办法吗?

更新 它没有显示任何错误。但是 table RealisasiDeadline 中的值不会改变,只有 Realisasi 中的值会改变。

为原始函数添加了一些注释,以便您可以对其进行改进。这段代码最大的问题是看着它我不知道它做了什么。

public function actionSimpanEdit($kode) {
        Yii::import('editable.EditableSaver'); // Should be at the top of the file
        // For the love of god use descriptive variable names
        $es = new EditableSaver($_GET['model']); // Would prefer to have model as actions argument
        $es->update();

        $es2 = RealisasiDeadline::model()->findByPk($kode); // no idea what this model is responsible for
        $es2['t1701'] = '1991-11-19'; // no idea what attribute t1701 is, use descriptive names
        $es->update();
    }

我对它进行了一些重构。仍然不知道它的作用;/

public function actionSimpanEdit($id, $model) {
        $editableSaver = new EditableSaver($model);
        $editableSaver->update();

        $deadline = RealisasiDeadline::model()->findByPk($id);

        if($deadline instanceof RealisasiDeadline) {
            $deadline->t1701 = '1991-11-19';
            if(!$deadline->update()) {
                // something went wrong
            }
        } else {
            // not found
        }
    }

回到你的问题。这可能是由于找不到RealisasiDeadline 模型或某些行为或事件导致它无法更新。