public function findByPk($pk, $condition = '', $params = array()) { if ($pk === 0) { return self::getDefaultStore(); } else { return parent::findByPk($pk, $condition, $params); } }
/** * 重写findByPk方法 */ public function findByPk($pk, $condition = '', $params = array()) { $key = $this->getCacheKey($pk); $cache = Yii::app()->cache->get($key); if ($cache === false) { $cache = parent::findByPk($pk, $condition, $params); Yii::app()->cache->set($key, $cache, $this->duration, $this->_getDependency()); } return $cache; }
/** * 重写findByPk方法,如果缓存存在数据,则直接读取缓存 * 在YiicmsActiveRecordBehavior中,一旦有数据更新则删除缓存 */ public function findByPk($pk, $condition = '', $params = array()) { $key = $this->getCacheKey($pk); //Yii::app()->cache->delete($key); $resource = Yii::app()->cache->get($key); if ($resource === false) { $resource = parent::findByPk($pk, $condition, $params); Yii::app()->cache->set($key, $resource, 3600); // 因为在缓存中没找到,重新生成 $value // 再缓存一下以备下次使用 // Yii::app()->cache->set($id,$value); } return $resource; }
/** * 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!')); } }
/** * * @param mixed $pk * @param string $condition * @param array $params * @return CStubActiveRecord */ public function findByPk($pk, $condition = '', $params = array()) { if (CStubActiveRecord::isUnittests()) { return CallFactory::call($this, 'findByPk'); } return parent::findByPk($pk, $condition, $params); }
public function findByPk($pk, $condition = '', $params = array()) { if (empty($condition) && empty($params)) { if (array_key_exists($pk, $this->findByPkCache)) { return $this->findByPkCache[$pk]; } else { $result = parent::findByPk($pk, $condition, $params); if (!is_null($result)) { $this->findByPkCache[$pk] = $result; } return $result; } } return parent::findByPk($pk, $condition, $params); }
public function findByPk($pk, $condition = '', $params = array()) { return $this->prepareRecord(parent::findByPk($pk, $this->prepareCondition($condition), $params)); }
/** * @see CActiveRecord::findByPk() * @return CEntity */ public function findByPk($pk, $condition = '', $params = array()) { $this->setDbReading(); if (($result = $this->readFromCache($pk)) !== false) { $this->setReadingFromCache(); $record = $this->populateRecord($result, true); $this->resetReadingFromCache(); return $record; } else { $this->resetReadingFromCache(); return parent::findByPk($pk); } }