CDbStatePersister

Package system.base
Inheritance class CDbStatePersister » CApplicationComponent » CComponent
Implements IApplicationComponent, IStatePersister
Since 1.1.17
Source Code 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.

Public Properties

Property Type Description Defined By
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

Public Methods

Method Description Defined By
__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

Protected Methods

Method Description Defined By
createTable() Creates state persister table CDbStatePersister

Property Details

db property

public CDbConnection $db;

instance

dbComponent property

public string $dbComponent;

connection ID

keyField property

public string $keyField;

Column name for key-field

stateTableName property

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 property

public string $valueField;

Column name for value-field

Method Details

createTable() method

protected void createTable()
Source Code: framework/base/CDbStatePersister.php#137 (show)
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() method

public mixed exists()
{return} mixed
Source Code: framework/base/CDbStatePersister.php#123 (show)
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() method

public void init()
Source Code: framework/base/CDbStatePersister.php#64 (show)
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() method

public mixed load()
{return} mixed state data. Null if no state data available.
Source Code: framework/base/CDbStatePersister.php#86 (show)
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() method

public int save(mixed $state)
$state mixed state data (must be serializable).
{return} int
Source Code: framework/base/CDbStatePersister.php#105 (show)
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.

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc/api/1.1/CDbStatePersister