加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – yii从数据库管理配置

发布时间:2020-12-13 22:44:10 所属栏目:PHP教程 来源:网络整理
导读:我正在研究yii框架.我有tbl_setting表和Setting模型.其中有许多键和值.管理员可以从管理面板更改所有值.表结构如下所示: define Value COMPANY_NAME Google META_TITLE .::My Site::. ........ ........ 在核心php我使用define()定义所有键值,在yii中如何全
我正在研究yii框架.我有tbl_setting表和Setting模型.其中有许多键和值.管理员可以从管理面板更改所有值.表结构如下所示:

define           Value     
 COMPANY_NAME     Google    
 META_TITLE       .::My Site::.
 ........  
 ........

在核心php我使用define()定义所有键值,在yii中如何全局使用它?

我试图在main.php文件中设置params但我不能在那里使用Setting模型.

我找到了答案.我已经使用了以下方法.我不确定这是不是好习惯,如果有人知道其他好方法请发帖.

创建了新组件: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'),

现在我可以使用以下方式访问所有值:

echo Yii::app()->setting->getValue('META_TITLE'); 
 echo Yii::app()->setting->getValue('COMPANY_ADDRESS');

解决方法

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

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'),

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读