extending CApplicationComponent




<?php

class EConfig extends CApplicationComponent {


	private static $cache;


	public function get($key) {

		if (null === self::$cache) {

			$configs = Yii::app()->db->createCommand('SELECT * FROM configs')->query();

			while (false !== ($row = $configs->read()))

			{

				self::$cache[$row['key']] = $row['value'];

			}

		}

		

		return self::$cache[$key];

	}


}



Hi, I have the above class which I made as a component. I used via Yii::app()->config->get(‘aaa’))

However I realised that self::$cache is almost always null. Meaning on the first call Yii::app()->config->get(‘aaa’)) $cache is always null.

I wanted it to be such that I do not have to query the database everytime for the values. Am I coding it wrongly?

I suggest u create new class not extended Yii classes and make it singleton. In this class create an array which holds all config with key => value. So while creating the singleton object full the array, then all time u can use this singleton object. it is important u to make it singleton. so call to db table data occurs only one time, while creating object.

Thanks. How about the case where there are new rows added to db, or changes in the values? How do I make the singleton to query the database again?