php中yii中数据库配置文件,php – yii从数据库管理配置

您可以添加新的应用程序组件

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();

}

}

}

之后你可以用它来向数据库添加param(如果还没有这样的param)

Yii::app()->config->add(array(

'param' => 'PARAMNAME',

'label' => 'yourlabel',

'value' => 4534,

'type' => 'integer',

'default' => 4534,

));

获得参数价值 –

Yii::app()->config->get('PARAMNAME');

设置参数值(如果存在param)

Yii::app()->config->set('PARAMNAME',100500);

当然,您需要为它创建表,类和控制器

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 ;

并为配置添加一些更改

components=>array(

...

'config' => array(

'class' => 'application.extensions.components.EConfig',

// 'cache'=>3600,

),

并在config中将config添加到preload部分

'preload' => array(..,'config'),

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值