我正在研究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,
),
'preload' => array(..,'config'),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。