TbEditableSaver::update PHP Method

update() public method

main function called to update column in database
public update ( )
    public function update()
    {
        //get params from request
        $this->primaryKey = yii::app()->request->getParam('pk');
        $this->attribute = yii::app()->request->getParam('name');
        $this->value = yii::app()->request->getParam('value');
        //checking params
        if (empty($this->attribute)) {
            throw new CException(Yii::t('TbEditableSaver.editable', 'Property "attribute" should be defined.'));
        }
        if (empty($this->primaryKey)) {
            throw new CException(Yii::t('TbEditableSaver.editable', 'Property "primaryKey" should be defined.'));
        }
        //loading model
        $this->model = CActiveRecord::model($this->modelClass)->findByPk($this->primaryKey);
        if (!$this->model) {
            throw new CException(Yii::t('TbEditableSaver.editable', 'Model {class} not found by primary key "{pk}"', array('{class}' => get_class($this->model), '{pk}' => is_array($this->primaryKey) ? CJSON::encode($this->primaryKey) : $this->primaryKey)));
        }
        //set scenario
        $this->model->setScenario($this->scenario);
        //commented to be able to work with virtual attributes
        //see https://github.com/vitalets/yii-bootstrap-editable/issues/15
        /*
        //is attribute exists
        if (!$this->model->hasAttribute($this->attribute)) {
        	throw new CException(Yii::t('EditableSaver.editable', 'Model {class} does not have attribute "{attr}"', array(
        	  '{class}'=>get_class($this->model), '{attr}'=>$this->attribute)));
        }
        */
        //is attribute safe
        if (!$this->model->isAttributeSafe($this->attribute)) {
            throw new CException(Yii::t('editable', 'Model {class} rules do not allow to update attribute "{attr}"', array('{class}' => get_class($this->model), '{attr}' => $this->attribute)));
        }
        //setting new value
        $this->setAttribute($this->attribute, $this->value);
        //validate attribute
        $this->model->validate(array($this->attribute));
        $this->checkErrors();
        //trigger beforeUpdate event
        $this->beforeUpdate();
        $this->checkErrors();
        //saving (no validation, only changed attributes)
        if ($this->model->save(false, $this->changedAttributes)) {
            //trigger afterUpdate event
            $this->afterUpdate();
        } else {
            $this->error(Yii::t('TbEditableSaver.editable', 'Error while saving record!'));
        }
    }

Usage Example

Example #1
0
 public function actionEditableSaver()
 {
     Yii::import('TbEditableSaver');
     $es = new TbEditableSaver('P3Media');
     // classname of model to be updated
     $es->update();
 }
All Usage Examples Of TbEditableSaver::update