Joomla 如何在J2.5中设置组件参数?
我使用组件的admin文件夹中的config.xml创建了一个J2.5组件,其中包含一些配置字段 如何以编程方式设置配置中的参数 我已经尝试了下面的代码,但它显然没有将结果保存到DB:Joomla 如何在J2.5中设置组件参数?,joomla,joomla2.5,Joomla,Joomla2.5,我使用组件的admin文件夹中的config.xml创建了一个J2.5组件,其中包含一些配置字段 如何以编程方式设置配置中的参数 我已经尝试了下面的代码,但它显然没有将结果保存到DB: $params = & JComponentHelper::getParams('com_mycomponent'); $params->set('myvar', $the_value); 任何人都可以展示一些如何实现这一点的例子吗?最安全的方法是包括com\u config/models/com
$params = & JComponentHelper::getParams('com_mycomponent');
$params->set('myvar', $the_value);
任何人都可以展示一些如何实现这一点的例子吗?最安全的方法是包括
com\u config/models/component.php
,并使用它来验证和保存参数。但是,如果您可以自己验证数据参数,我将坚持以下(更简单的解决方案):
//获取参数并设置新值
$params=JComponentHelper::getParams('com_mycomponent');
$params->set('myvar',$u值);
//获取新的数据库查询实例
$db=JFactory::getDBO();
$query=$db->getQuery(true);
//构建查询
$query->update(“#u扩展名为a”);
$query->set('a.params='。$db->quote((字符串)$params));
$query->where('a.element=“com_mycomponent”);
//执行查询
$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();
解决办法就在这里 您可以在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;
}