| Package | system.base |
|---|---|
| Inheritance | class CDbStatePersister » CApplicationComponent » CComponent |
| Implements | IApplicationComponent, IStatePersister |
| Since | 1.1.17 |
| Source Code | framework/base/CDbStatePersister.php |
| 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 |
| 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 |
| Method | Description | Defined By |
|---|---|---|
| createTable() | Creates state persister table | CDbStatePersister |
instance
connection ID
Column name for key-field
the database table name storing the state data. Make sure the table exists or database user is granted to CREATE tables.
Column name for value-field
|
protected void createTable()
|
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
|
public mixed exists()
| ||
| {return} | mixed | |
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();
}
|
public void init()
|
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::t ('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.
|
public mixed load()
| ||
| {return} | mixed | state data. Null if no state data available. |
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.
|
public int save(mixed $state)
| ||
| $state | mixed | state data (must be serializable). |
| {return} | int | |
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.
Signup or Login in order to comment.