Beispiel #1
0
 /**
  * Sets username fields of a model
  */
 public static function usernameFieldsSet(CActiveRecord $model, $username)
 {
     if ($model->hasAttribute('updatedBy')) {
         $model->updatedBy = $username;
     }
     if ($model->hasAttribute('createdBy') && $model->isNewRecord) {
         $model->createdBy = $username;
     }
 }
Beispiel #2
0
 public function hasAttribute($name)
 {
     //return true;
     $attr = get_object_vars($this);
     if (isset($attr[$name])) {
         return true;
     } else {
         return parent::hasAttribute($name);
     }
 }
Beispiel #3
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]);
     }
 }
Beispiel #4
0
 /**
  *### .getAttribute()
  *
  * Helper function to get an attribute from the data
  *
  * @param CActiveRecord $data
  * @param string $attribute the attribute to get
  *
  * @return mixed the attribute value null if none found
  */
 protected function getAttribute($data, $attribute)
 {
     if ($this->dataProvider instanceof CActiveDataProvider && $data->hasAttribute($attribute)) {
         return $data->{$attribute};
     }
     if ($this->dataProvider instanceof CArrayDataProvider || $this->dataProvider instanceof CSqlDataProvider) {
         if (is_object($data) && isset($data->{$attribute})) {
             return $data->{$attribute};
         }
         if (isset($data[$attribute])) {
             return $data[$attribute];
         }
     }
     return null;
 }
Beispiel #5
0
 public function hasAttribute($name)
 {
     if ($name == 'confirm_password') {
         return true;
     }
     return parent::hasAttribute($name);
 }