本文整理汇总了PHP中CActiveRecord::findByPk方法的典型用法代码示例。如果您正苦于以下问题:PHP CActiveRecord::findByPk方法的具体用法?PHP CActiveRecord::findByPk怎么用?PHP CActiveRecord::findByPk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CActiveRecord
的用法示例。
在下文中一共展示了CActiveRecord::findByPk方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: findByPk
public function findByPk($pk, $condition = '', $params = array())
{
if ($pk === 0) {
return self::getDefaultStore();
} else {
return parent::findByPk($pk, $condition, $params);
}
}
示例2: findByPk
/**
* 重写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;
}
示例3: findByPk
/**
* 重写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;
}
示例4: 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');
$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!'));
}
}
示例5: findByPk
/**
*
* @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);
}
示例6: findByPk
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);
}
示例7: findByPk
public function findByPk($pk, $condition = '', $params = array())
{
return $this->prepareRecord(parent::findByPk($pk, $this->prepareCondition($condition), $params));
}
示例8: findByPk
/**
* @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);
}
}