Yii 1.1: dbparam

Allows to store custom application params in the DB
6 followers

This extension provides a handy way to store and use application parameters such as caching time for components, site admin email, or other things. It is extremely useful when you need to provide interface for editing of these parameters. The out-of-box Yii distribution allows to use only configuration file, which is not perfect way to do it (yet much faster). Current extension allows you to configure parameters.

Resources

Documentation

Requirements

  • Yii 1.0 or above
  • Any supported DBMS (tested only with MySQL, but should work on others as well)

Installation

  • Extract the release file under protected/extensions
  • If your DBMS doesn't support BLOB, LONGBLOB etc type, you should:
    • Edit method createParamsTable to create the table type you need
    • Edit lines 302, 310 and change PDO::PARAM_LOB to the constant you need

Usage

Set up component in the application config:

'par'=>array(
    'class' => 'application.extensions.dbparam.XDbParam',
    'connectionID' => 'db',//id of the connection component, just the same as with CDbCache
//  'preload' => 'test,test2', //comma-separated string or array of params to be loaded anyway. Other params are loaded only when requested.
//  'autoLoad' => true,//loads all attributes when initializing
//  'caseSensitive' => true, //setting to true makes all parameters case sensitive
    ),

Use the component:

Yii::app()->par->test = '1234';//set parameter. If it is not present, it will be created
echo Yii::app()->par->test;//output parameter. If it is not present, exception is thrown
 
//load several parameters in one query. Useful if you're going to use them in the next lines
Yii::app()->par->load(array('test', 'test2'));
//OR
Yii::app()->par->load('test,test2');
//OR
Yii::app()->par->load();//loads all attributes
 
//delete the specified parameters or all of them if none specified
Yii::app()->par->purge('test,test2');//delete test and test 2
//OR
Yii::app()->par->purge(array('test', 'test3'));//delete test and test 3
//OR
Yii::app()->par->purge();//delete ALL parameters!

Change Log

March 4, 2009

  • Initial release.

March 5, 2009

  • Version 2.0:
    • Added autoloading of all parameters (autoLoad config option)
    • Added reloading of all parameters ($component->load() - calling without params loads all parameters)
    • Rewrote all __get/__set code to make it work faster
    • Changed hierarchy, it is now extended from CAttributesCollection and implements IApplicationComponent

Total 6 comments

#11915 report it
Cozumel at 2013/02/12 11:29pm
Serialize Data

To Serialize the data:

Line 267

public function __get($name)
    {
        if($this->contains($name))
            return unserialize($this->itemAt($name));
        else

Line 292

public function __set($name,$value)
    {
        $value = serialize($value);
 
        if (!$this->caseSensitive)
#1458 report it
MarcS at 2009/07/29 12:03pm
Serialize

Great extension

but wouldn't it make sense to serialize the values before saving so that you could also store arrays?

I made a few modifications to your code to do that. Works great

#1608 report it
KJedi at 2009/06/04 12:42am
Thanks!

Thanks a lot for such extensive code review! I don't have time for v3 now, but I've corrected all bugs you listed: 1) Yes, really critical, how could I miss it? :) 2) Yes, really... I should use unit testing next time. 3) Yes, I didn't realize that 3.1) Yes, it is not needed. Update is uploaded!

#1611 report it
Yeti at 2009/06/03 01:15pm
Great - but a couple of bugs

This review is for V2

Great extension but there a couple of minor bugs that (with one exception) don't stop your app working, but mean the extension is not being as efficient as it can be.

1 SQL syntax error

Issue: This does stop things working as it generates an SQL syntax error

Severity: major - it doesn't work

Fix: change line 331 from

$sql .= 'WHERE name IN (\''.implode('\',\'', $params).'\')';

to

$sql .= ' WHERE name IN (\''.implode('\',\'', $params).'\')';

(add a space before WHERE)

2 Problem with case sensitivity

Issue: Parameters are being cached case insensitively and read case sensitively. If parameters have uppercase letters this means there is never a cache hit and the parameter is always read from the DB instead of the cache for second and subsequent reads, resulting in additional queries.

Severity: minor - you always get the parameter.

Fix: Change lines 212 and 295 to read

if (!$this->caseSensitive)

(add ! before $this)

3 preprocessParams() not handling null correctly

Issue: If there are no preload parameters, preprocessParameters() receives null from init() at line 117 but returns a non-empty array meaning the test at line 118 is always true.

Severity: very minor - one extra query per request

At line 238, !is_array(null) === true, which means line 240, $params = explode(',', $params);, is executed.

explode(null) results in an array with a single entry of an empty string.

When this is passed back to init() the test at line 118 is always true as $this->preload is never empty, which means the preload query is run even when not needed.

Fix: Change line 238 from

if (!is_array($params))

to

if (is_string($params))

Only strings are exploded leaving null untouched, which means the test at line 118 will give the expected result and not run the query if not needed.

I also think lines 246 and 247 are not needed; $preload is not declared in the scope of the method.

With these fixes the extension does just what I need; thanks. Looking forward to V3.

#1674 report it
jz at 2009/05/14 09:37pm
great ext, thanks!

Удачи! ;)

#1760 report it
megabr at 2009/04/14 05:35pm
thanks

ok

Leave a comment

Please to leave your comment.

Create extension
Downloads
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: KJedi
  • Category: Others
  • Votes: +9
  • Downloaded: 1,093 times
  • Created on: Mar 4, 2009
  • Last updated: Mar 5, 2009