Search code examples
phpyiiyii-extensionsx-editable

Update two models using yii x-editable


So, I'm using this extension: x-editable for yii.

And I'm currently trying to update two models in update() function.

I have two models:

Realisasi.php

RealisasiDeadline.php.

So when a cell is updated on table Realisasi.php (one value in column t1701 in this case), I want the function to update the corresponding value in column t1701 of table RealisasiDeadline, using column no as the foreign key.

Since I haven't found any example on Google, I made it up myself:

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
}

This is the 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')),
        )
    ),

What have I missed? Is it possible at all to do? If not, is there another solution?

UPDATE It's not showing any error. But the value in table RealisasiDeadline doesn't change, only the one in Realisasi does.


Solution

  • Added some comments to original function so you can improve upon it. Biggest issue with this code is that looking at it I have no idea what it does.

    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();
        }
    

    I have refactored it a bit. Still have no idea what it does ;/

    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
            }
        }
    

    Going back to your problem. It is probably caused by RealisasiDeadline model being not found or some behavior or event preventing it from update.