2012-06-21 2 views
4

저는 yii 프레임 워크에서 일하고 있습니다. 나는 tbl_setting 테이블과 설정 모델입니다. 많은 키와 값을 가지고 있습니다. 관리자는 관리자 패널에서 모든 값을 변경할 수 있습니다. 난 YII에()를 정의하여 모든 키 값을 규정 한 코어 PHP에서yii 데이터베이스에서 구성을 관리하십시오.

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

어떻게 세계적 사용할 수 테이블 구조는 아래와 같다?

main.php 파일에서 params를 설정하려고했지만 Setting Model을 사용할 수 없습니다.


답변을 찾았습니다. 나는 아래의 접근법을 사용했다. 나는 이것이 좋은 연습인지 아닌지 잘 모르겠다. 누군가 다른 좋은 방법을 알고 있으면 게시 해주세요.

만든 새 구성 요소 : 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'); 
+0

왜 주 구성 파일에 설정하려고합니까? 이 설정은 무엇입니까? – Orlymee

+1

http://www.yiiframework.com/wiki/187/how-to-write-a-simple-application-component – adamors

+0

@Orlymee 모든 글로벌 설정이 가능합니다. 즉 관리자는 전면 페이지의 메타 제목을 설정할 수 있습니다. @ ors 나는 그것을 검사하고있다. – VibhaJ

답변

0
내가 사용했다

,

<?php 
class SitesettingWidget extends CWidget { 

    public function run() { 
     $siteData = SiteSetting::model()->findAll(); 

     $this->render('setting', array(
      'siteData'=>$siteData, 
     )); 
    } 
} 

그러나 CApplicationComponent 더 나은 방법이라고 생각 ...

1

당신은 새로운 응용 프로그램 구성 요소 그 후

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