3

コンポーネントのadminフォルダーにあるconfig.xmlを使用して、いくつかの構成フィールドを持つJ2.5コンポーネントを作成しました。

プログラムで設定にパラメータを設定するにはどうすればよいですか?

次のコードを試しましたが、結果がDBに保存されないことは明らかです。

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

誰かがこれを達成する方法のいくつかの例を示してもらえますか?

4

2 に答える 2

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();

paramsを文字列にキャストする方法(クエリを作成するとき)に注意してください。これにより、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();
于 2012-11-04T18:21:55.143 に答える
1

解決策はここにあります...

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

Joomla2.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;
}
于 2014-12-10T15:32:15.743 に答える