Пример #1
0
 /**
  *### .update()
  *
  * main function called to update column in database
  *
  */
 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!'));
     }
 }
Пример #2
0
 /**
  * main function called to update column in database
  *
  */
 public function update()
 {
     //set params from request
     $this->primaryKey = yii::app()->request->getParam('pk');
     $this->attribute = yii::app()->request->getParam('name');
     $value = Yii::app()->request->getParam('value');
     //checking params
     if (empty($this->attribute)) {
         throw new CException(Yii::t('zii', 'Property "attribute" should be defined.'));
     }
     if (empty($this->primaryKey)) {
         throw new CException(Yii::t('zii', 'Property "primaryKey" should be defined.'));
     }
     //loading model
     $this->model = CActiveRecord::model($this->modelClass)->findByPk($this->primaryKey);
     if (!$this->model) {
         throw new CException(Yii::t('editable', 'Model {class} not found by primary key "{pk}"', array('{class}' => get_class($this->model), '{pk}' => $this->primaryKey)));
     }
     $this->model->setScenario($this->scenario);
     //is attribute exists
     if (!$this->model->hasAttribute($this->attribute)) {
         throw new CException(Yii::t('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('zii', '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, $value);
     //validate
     $this->model->validate(array($this->attribute));
     if ($this->model->hasErrors()) {
         $this->error($this->model->getError($this->attribute));
     }
     //save
     if ($this->beforeUpdate()) {
         //saving (only chnaged attributes)
         if ($this->model->save(false, $this->changedAttributes)) {
             $this->afterUpdate();
         } else {
             $this->error(Yii::t('zii', 'Error while saving record!'));
         }
     } else {
         $firstError = reset($this->model->getErrors());
         $this->error($firstError[0]);
     }
 }
Пример #3
0
 /**
  * main function called to update column in database
  *
  */
 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');
     $this->scenario = yii::app()->request->getParam('scenario');
     //checking params
     if (empty($this->attribute)) {
         throw new CException(Yii::t('EditableSaver.editable', 'Property "attribute" should be defined.'));
     }
     $this->model = new $this->modelClass();
     $isFormModel = $this->model instanceof CFormModel;
     $isMongo = EditableField::isMongo($this->model);
     if (empty($this->primaryKey) && !$isFormModel) {
         throw new CException(Yii::t('EditableSaver.editable', 'Property "primaryKey" should be defined.'));
     }
     //loading model
     if ($isMongo) {
         $this->model = $this->model->findByPk(new MongoID($this->primaryKey));
     } elseif (!$isFormModel) {
         $this->model = $this->model->findByPk($this->primaryKey);
     }
     if (!$this->model) {
         throw new CException(Yii::t('EditableSaver.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)));
     }
     //keep parent model for mongo
     $originalModel = $this->model;
     //resolve model only for mongo! we should check attribute safety
     if ($isMongo) {
         $resolved = EditableField::resolveModels($this->model, $this->attribute);
         $this->model = $resolved['model'];
         //can be related model now
         $this->attribute = $resolved['attribute'];
         $staticModel = $resolved['staticModel'];
     } else {
         $staticModel = $this->model;
     }
     //set scenario for main model
     if ($this->scenario) {
         $originalModel->setScenario($this->scenario);
     }
     //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();
     //remove virtual attributes (which NOT in DB table)
     if (!$isMongo) {
         $this->changedAttributes = array_intersect($this->changedAttributes, $originalModel->attributeNames());
         if (count($this->changedAttributes) == 0) {
             //can not pass empty array in model->save() method!
             $this->changedAttributes = null;
         }
     }
     //saving (no validation, only changed attributes) note: for mongo save all!
     if ($isMongo) {
         $result = $originalModel->save(false, null);
     } elseif (!$isFormModel) {
         $result = $originalModel->save(false, $this->changedAttributes);
     } else {
         $result = true;
     }
     if ($result) {
         $this->afterUpdate();
     } else {
         $this->error(Yii::t('EditableSaver.editable', 'Error while saving record!'));
     }
 }