discuss.answer
You want to create a single
lexicon entry:
$entry = $modx->newObject('modLexiconEntry');
$entry->set('name', 'keyName');
$entry->set('value', 'something');
$entry->set('topic', 'topicName');
$entry->set('namespace', 'namespaceName');
$entry->save();
This will have no effect on the lexicon files, but the DB changes will survive any upgrades. It will slow things down a tiny bit because MODX will load both the lexicon filed and the DB entries on every page load that calls for that namespace and topic.
Thank you Bob.
And if I want to test if an entry exist, do I use:
$entry = $modx->getObject('modLexiconEntry',
array('name'=>'keyName',
'topic'=>'topicName',
'namespace'=>'namespaceName')
);
$entry->set('value', 'something');
$entry->save();
or do you suggest another method to update or create in one step instead of testing if exist and update if yes or create if not?
update or create in one step, could look like that:
$fields = array(
'name' => 'keyName',
'topic' => 'topicName',
'namespace' => 'namespaceName');
if ($entry = $modx->getObject('modLexiconEntry', $fields)) {
} else {
$entry = $modx->newObject('modLexiconEntry');
$entry->fromArray($fields);
}
$entry->set('value', 'something');
$entry->save();