【问题标题】:How to set Component parameters in J2.5?J2.5中如何设置组件参数?
【发布时间】:2012-11-04 15:12:14
【问题描述】:

我使用组件管理文件夹中的 config.xml 创建了一个带有一些配置字段的 J2.5 组件。

如何以编程方式在配置中设置参数?

我已经尝试了下面的代码,但它显然没有将结果保存到数据库:

$params = & JComponentHelper::getParams('com_mycomponent');
$params->set('myvar', $the_value);

任何人都可以举一些例子来说明如何实现这一点吗?

【问题讨论】:

    标签: joomla joomla2.5


    【解决方案1】:

    最安全的方法是包含com_config/models/component.php 并使用它来验证和保存参数。但是,如果您可以以某种方式自己验证数据参数,我会坚持以下(更简单的解决方案):

    // Get the params and set the new values
    $params = JComponentHelper::getParams('com_mycomponent');
    $params->set('myvar', $the_value);
    
    // Get a new database query instance
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    
    // Build the query
    $query->update('#__extensions AS a');
    $query->set('a.params = ' . $db->quote((string)$params));
    $query->where('a.element = "com_mycomponent"');
    
    // Execute the query
    $db->setQuery($query);
    $db->query();
    

    注意我如何将参数转换为字符串(在构建查询时),它会将 JRegistry 对象转换为 JSON 格式的字符串。

    如果您遇到任何缓存问题,您可能希望在编辑参数后运行以下命令:

    来自模型:

     $this->cleanCache('_system');
    

    或者,其他地方:

    $conf = JFactory::getConfig();
    
    $options = array(
        'defaultgroup' => '_system',
        'cachebase' => $conf->get('cache_path', JPATH_SITE . '/cache')
    );
    
    $cache = JCache::getInstance('callback', $options);
    $cache->clean();
    

    【讨论】:

      【解决方案2】:

      解决方案在这里...

      http://www.webtechriser.com/tutorials/82-joomla-3-0/86-how-to-save-component-parameters-to-database-programmatically

      您可以在 Joomla 2.5+ 中替换

      // check for error
      if (!$table->check()) {
          $this->setError('lastcreatedate: check: ' . $table->getError());
          return false;
      }
      if (!$table->store()) {
          $this->setError('lastcreatedate: store: ' . $table->getError());
          return false;
      }
      

      if (!$table->save()) {
          $this->setError('Save Error: ' . $table->getError());
          return false;
      }
      

      【讨论】:

      • 在此之后,我可以在数据库表中看到更新配置,但它没有反映。它可以通过清理系统缓存来解决。
      猜你喜欢
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多