serializable-yii-db-command Serializable CDbCommand / CDbConnection derivative

  1. Requirements
  2. Usage
  3. Caveats
  4. Resources

This is a Yii extension that allows a user to freeze and thaw a CDbCommand. This could be useful for, as an example, being able to "freeze" a report that might take a while, send the job to a queueing system and have it picked up by a worker process (perhaps a Yii command line process) that would then "thaw" the job and execute it asynchronously. This would give you the ability to have one method for creating reports, and makes it trivial to either process it synchronously within the request or farm it out.


Yii 1.1 or above, php 5.3 or above



This class extends CDbConnection, and adds the following parameter:


This parameter is used to signal what the connection name is on the thawing side. Defaults to 'db'. As an example:

'db' => array(
            'class' => 'ESDbConnection',
            'connectionName' => 'db',
            'connectionString' => ';dbname=webscale',
            'emulatePrepare' => false,
            'username' =>'bob',
            'password' => 'dobbs',
            'charset' => 'latin1',
            'enableProfiling' => true,
            'enableParamLogging' => true,

It also overrides the createCommand method to return an ESDbCommand object versus an CDbCommand object.

Which brings us to:


This is where the "magic" happens. This class is where the actual serialization takes place. This is a drop-in replacement for CDbCommand, with the exception that this will survive a serialize and unserialize round trip. It uses the ESDbConnection 'connectionName' to know what connection to get from the config, preventing redundant connections to the database.

As long as you've configured a database connection with the ESDbConnection as above, to use ESDbCommand, you simply:

$command = Yii::app()->db->createCommand();


It is important to note that variables bound with bindParam() and bindParams() are converted to their corresponding bindValue() call due to the fact that variables bound by reference (such as with bindParam) will not survive the serialization round trip. However, if you're planning on serializing a CDbCommand, you should probably avoid bindParam / bindParams anyway. The current behavior is to take the value of variable the bind parameter references at the time of serialization, and then call bindValue() on it upon unserialize.

TLDR; don't use bindParam(s), use bindValue(s) instead.


0 0
Yii Version: Unknown
License: BSD-2-Clause
Category: Database
Developed by: boingolover
Created on: Apr 12, 2014
Last updated: 6 years ago


show all

Related Extensions