Reputation: 2256
I am working on yii framework. I have tbl_setting table and Setting model. Which have many keys and values. Admin can change all values from admin panel. Table structure is shown below:
define Value
COMPANY_NAME Google
META_TITLE .::My Site::.
........
........
In core php i was defining all key values using define(), in yii how can i use it globally?
I was trying to set in params on main.php file but i can not use Setting model there.
I found the answer. I have done using below approach. I am not sure if this is the good practice or not, if anyone know other good way please post.
Created new component: WebSetting.php
class WebSetting extends CApplicationComponent
{
function getValue($key)
{
$model = Setting::model()->findByAttributes(array('define'=>$key));
return $model->value;
}
}
main.php
'setting'=>array('class'=>'WebSetting'),
And now i can access all values anywhere using:
echo Yii::app()->setting->getValue('META_TITLE');
echo Yii::app()->setting->getValue('COMPANY_ADDRESS');
Upvotes: 4
Views: 3601
Reputation: 59
You can add a new application component
class EConfig extends CApplicationComponent
{
public $cache = 0;
public $dependency = null;
protected $data = array();
public function init()
{
$items=Config::model()->findAll();
foreach ($items as $item)
{
if ($item['param'])
$this->data[$item['param']] = $item['value'] === '' ? $item['default'] : $item['value'];
}
parent::init();
}
public function get($key)
{
if (array_key_exists($key, $this->data))
return $this->data[$key];
else
//throw new CException('Undefined parameter ' . $key);
return '';
}
public function set($key, $value)
{
$model = Config::model()->findByAttributes(array('param'=>$key));
if (!$model)
throw new CException('Undefined parameter ' . $key);
$model->value = $value;
if ($model->save())
$this->data[$key] = $value;
}
public function add($params)
{
if (isset($params[0]) && is_array($params[0]))
{
foreach ($params as $item)
$this->createParameter($item);
}
elseif ($params)
$this->createParameter($params);
}
public function delete($key)
{
if (is_array($key))
{
foreach ($key as $item)
$this->removeParameter($item);
}
elseif ($key)
$this->removeParameter($key);
}
protected function getDbConnection()
{
if ($this->cache)
$db = Yii::app()->db->cache($this->cache, $this->dependency);
else
$db = Yii::app()->db;
return $db;
}
protected function createParameter($param)
{
if (!empty($param['param']))
{
$model = Config::model()->findByAttributes(array('param' => $param['param']));
if ($model === null)
$model = new Config();
$model->param = $param['param'];
$model->label = isset($param['label']) ? $param['label'] : $param['param'];
$model->value = isset($param['value']) ? $param['value'] : '';
$model->default = isset($param['default']) ? $param['default'] : '';
$model->type = isset($param['type']) ? $param['type'] : 'string';
$model->save();
}
}
protected function removeParameter($key)
{
if (!empty($key))
{
$model = Config::model()->findByAttributes(array('param'=>$key));
if ($model)
$model->delete();
}
}
}
After that you can use it for Adding param to database (if there is not such param yet)
Yii::app()->config->add(array(
'param' => 'PARAMNAME',
'label' => 'yourlabel',
'value' => 4534,
'type' => 'integer',
'default' => 4534,
));
Getting param value -
Yii::app()->config->get('PARAMNAME');
Setting param value (if param exists)
Yii::app()->config->set('PARAMNAME',100500);
Of course you will need to create table and class and controller for it
CREATE TABLE `Config` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`param` varchar(128) NOT NULL,
`value` text NOT NULL,
`default` text NOT NULL,
`label` varchar(255) NOT NULL,
`type` varchar(128) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `param` (`param`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=290 ;
And add some changes to config
components=>array(
...
'config' => array(
'class' => 'application.extensions.components.EConfig',
// 'cache'=>3600,
),
And add config to preload section in config
'preload' => array(..,'config'),
Upvotes: 1
Reputation: 1210
I was using,
<?php
class SitesettingWidget extends CWidget {
public function run() {
$siteData = SiteSetting::model()->findAll();
$this->render('setting', array(
'siteData'=>$siteData,
));
}
}
But think CApplicationComponent is better way...
Upvotes: 0