system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

CDbStatePersister

system.base
继承 class CDbStatePersister » CApplicationComponent » CComponent
实现 IApplicationComponent, IStatePersister
可用自 1.1.17
源码 framework/base/CDbStatePersister.php
CDbStatePersister implements a database persistent data storage.

It can be used to keep data available through multiple requests and sessions.

By default, CDbStatePersister stores data in a table named 'state'. You may change the location by setting the stateTableName property.

To retrieve the data from CDbStatePersister, call load(). To save the data, call save().

Comparison among state persister, session and cache is as follows:
  • session: data persisting within a single user session.
  • state persister: data persisting through all requests/sessions (e.g. hit counter).
  • cache: volatile and fast storage. It may be used as storage medium for session or state persister.

公共属性

隐藏继承的属性

属性类型描述被定义在
behaviors array the behaviors that should be attached to this component. CApplicationComponent
db CDbConnection instance CDbStatePersister
dbComponent string connection ID CDbStatePersister
isInitialized boolean Checks if this application component has been initialized. CApplicationComponent
keyField string Column name for key-field CDbStatePersister
stateTableName string the database table name storing the state data. CDbStatePersister
valueField string Column name for value-field CDbStatePersister

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
exists() CDbStatePersister
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getIsInitialized() Checks if this application component has been initialized. CApplicationComponent
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the component. CDbStatePersister
load() Loads state data from persistent storage. CDbStatePersister
raiseEvent() Raises an event. CComponent
save() Saves application state in persistent storage. CDbStatePersister

受保护的方法

隐藏继承的方法

方法描述被定义在
createTable() Creates state persister table CDbStatePersister

属性详情

db 属性
public CDbConnection $db;

instance

dbComponent 属性
public string $dbComponent;

connection ID

keyField 属性
public string $keyField;

Column name for key-field

stateTableName 属性
public string $stateTableName;

the database table name storing the state data. Make sure the table exists or database user is granted to CREATE tables.

valueField 属性
public string $valueField;

Column name for value-field

方法详情

createTable() 方法
protected void createTable()
源码: framework/base/CDbStatePersister.php#137 (显示)
protected function createTable()
{
    try
    {
        
$command=$this->db->createCommand();
        
$command->createTable($this->stateTableName,array(
            
$this->keyField=>'string NOT NULL',
            
$this->valueField=>'text NOT NULL',
            
'PRIMARY KEY ('.$this->db->quoteColumnName($this->keyField).')'
        
));
    }
    catch (
CDbException $e)
    {
        throw new 
CException(Yii::t('yii','Can\'t create state persister table. Check CREATE privilege for \'{db}\' connection user or create table manually with SQL: {sql}.',array('{db}'=>$this->dbComponent,'{sql}'=>$command->text ) ) );
    }
}

Creates state persister table

exists() 方法
public mixed exists()
{return} mixed
源码: framework/base/CDbStatePersister.php#123 (显示)
public function exists()
{
    
$command=$this->db->createCommand();
    
$command=$command->select($this->keyField)->from($this->stateTableName);
    
$command=$command->where($this->db->quoteColumnName($this->keyField).'=:key',array(
        
':key'=>Yii::app()->name
    
));
    return 
$command->queryScalar();
}

init() 方法
public void init()
源码: framework/base/CDbStatePersister.php#64 (显示)
public function init()
{
    
parent::init();
    if(
$this->stateTableName===null)
        throw new 
CException(Yii::t('yii''stateTableName param cannot be null.'));
    
$this->db=Yii::app()->getComponent($this->dbComponent);
    if(
$this->db===null)
        throw new 
CException(Yii::t('yii''\'{db}\' component doesn\'t exist.',array(
            
'{db}'=>$this->dbComponent
        
)));
    if(!(
$this->db instanceof CDbConnection))
        throw new 
CException(Yii::('yii''\'{db}\' component is not a valid CDbConnection instance.',array(
            
'{db}'=>$this->dbComponent
        
)));
    if(
$this->db->schema->getTable($this->stateTableName,true)===null)
        
$this->createTable();
}

Initializes the component. This method overrides the parent implementation by making sure stateFile contains valid value.

load() 方法
public mixed load()
{return} mixed state data. Null if no state data available.
源码: framework/base/CDbStatePersister.php#86 (显示)
public function load()
{
    
$command=$this->db->createCommand();
    
$command=$command->select($this->valueField)->from($this->stateTableName);
    
$command=$command->where($this->db->quoteColumnName($this->keyField).'=:key',array(
        
':key'=>Yii::app()->name
    
));
    
$state=$command->queryScalar();
    if(
false!==$state)
        return 
unserialize($state);
    else
        return 
null;
}

Loads state data from persistent storage.

save() 方法
public int save(mixed $state)
$state mixed state data (must be serializable).
{return} int
源码: framework/base/CDbStatePersister.php#105 (显示)
public function save($state)
{
    
$command=$this->db->createCommand();
    if(
false===$this->exists())
        return 
$command->insert($this->stateTableName,array(
            
$this->keyField=>Yii::app()->name,
            
$this->valueField=>serialize($state)
        ));
    else
        return 
$command->update($this->stateTableName,array($this->valueField=>serialize($state)),
            
$this->db->quoteColumnName($this->keyField).'=:key',
            array(
':key'=>Yii::app()->name)
    );
}

Saves application state in persistent storage.