We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17013
    • 5 Posts
    An external script have to update or create a lexicon entry using modx as API. The context and the lang exist.
    To retrieve a lexicon value, I can use $modx->lexicon->load but how can I update or create a lexicon entry for a specific language? $modx->lexicon->set with as described on http://api.modx.com/revolution/2.2/db_core_model_modx_modlexicon.class.html ?

    $modx->getService('lexicon','modLexicon');
    $modx->lexicon->set('mykey',myvalue');

    is it the right way or must I use another way?

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      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.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 17013
        • 5 Posts
        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?
          • 4172
          • 5,888 Posts
          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();
          
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!