Another Application.params component

I like the idea of using Yii::app()->params to access my params. But I wanted to store them in DB. Sure, loading everything for each call is not a good idea, so I was thinking of creating my own component for this.

So I created simple class, just to test my idea:

<?php


class CParams implements IApplicationComponent 


{


    protected $i = false;


    


    public function __get($prop)


    {


        return $prop.' => '.rand(1,1000);


    }


    


    public function getIsInitilized()


    {


        return $this->i;


    }


    


    public function init()


    {


        $this->i = true;


    }


}


?>

and call this from controller:

public function actionTest()


    {


        echo Yii::app()->params->d;


        echo Yii::app()->params->e;


    }

Here is config for the params application component:

'params'=>array(


        'class' => 'application.components.CParams'


    ),

But I get error:

Property "CAttributeCollection.d" is not defined.

And the stack trace:

Quote

[pre]#0 E:\XAMPP\htdocs\Yii\collections\CAttributeCollection.php(52): CComponent->__get('d')

#1 E:\XAMPP\htdocs\EveryStyle\protected\controllers\TestController.php(6): CAttributeCollection->__get('d')

#2 E:\XAMPP\htdocs\Yii\web\actions\CInlineAction.php(32): TestController->actionTest()

#3 E:\XAMPP\htdocs\Yii\web\CController.php(265): CInlineAction->run()

#4 E:\XAMPP\htdocs\Yii\web\CController.php(243): CController->runAction(Object(CInlineAction))

#5 E:\XAMPP\htdocs\Yii\web\CController.php(225): CController->runActionWithFilters(Object(CInlineAction), Array)

#6 E:\XAMPP\htdocs\Yii\web\CWebApplication.php(335): CController->run('test')

#7 E:\XAMPP\htdocs\Yii\web\CWebApplication.php(123): CWebApplication->runController('test/test')

#8 E:\XAMPP\htdocs\Yii\base\CApplication.php(170): CWebApplication->processRequest()

#9 E:\XAMPP\htdocs\EveryStyle\index.php(11): CApplication->run()

#10 {main}[/pre]

So it means, that my component is not substituted there! Why?

'params' is not an application component. You need to choose a different name for it.

Yes, really :) I didn’t notice that. It works pretty good now. I’ll share the solution when it’s ready.

Thanks. This could be a very useful extension.

Here is an extension:

http://www.yiiframew…ension/dbparam/

Here is a post in my blog about it:

http://programmersno…g-the-database/