당신은 새로운 응용 프로그램 구성 요소 그 후
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,
));
얻기 PARAM 값 -
Yii::app()->config->get('PARAMNAME');
설정 PARAM 값 (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'),
왜 주 구성 파일에 설정하려고합니까? 이 설정은 무엇입니까? – Orlymee
http://www.yiiframework.com/wiki/187/how-to-write-a-simple-application-component – adamors
@Orlymee 모든 글로벌 설정이 가능합니다. 즉 관리자는 전면 페이지의 메타 제목을 설정할 수 있습니다. @ ors 나는 그것을 검사하고있다. – VibhaJ