如何在J2.5中设置组件参数?

Sti*_*ero 3 joomla joomla2.5

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

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

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

$params = & JComponentHelper::getParams('com_mycomponent');
$params->set('myvar', $the_value);
Run Code Online (Sandbox Code Playgroud)

有谁能请展示一些如何实现这一目标的例子?

Mat*_*röm 11

最安全的方法是包含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();
Run Code Online (Sandbox Code Playgroud)

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

如果遇到任何缓存问题,可能需要在编辑params后运行以下命令:

从模型:

 $this->cleanCache('_system');
Run Code Online (Sandbox Code Playgroud)

或者,否则在哪里:

$conf = JFactory::getConfig();

$options = array(
    'defaultgroup' => '_system',
    'cachebase' => $conf->get('cache_path', JPATH_SITE . '/cache')
);

$cache = JCache::getInstance('callback', $options);
$cache->clean();
Run Code Online (Sandbox Code Playgroud)