[MODX 2.8] Change setting keys from CLI : possible?

Hi :slight_smile:

Is there a way to change setting keys inside MODX 2.8.1 from CLI ?
I’m looking for a way to improve my new MODX installations with my install script.

If your script is in PHP and you’ve initialized MODX, then you should be able to do this:

$settings = array(
   'setting1_key' => 'some value',
   'setting2_key' => 'some value',
   // etc.
);

foreach($settings as $key => $value) {
    $setting = $modx->getObject('modSystemSetting', array ('key' => $key));
    if ($setting) {
        $setting->set('value', $value);
        $setting->save();
    } else {
        $modx->log(modX::LOG_LEVEL_ERROR, 
            'Could not find setting: '  . $key);
    }
}

That’s assuming that they’re System Settings. If they’re user, usergroup, or context settings, it gets more complicated but still doable.

1 Like

Thanks !
But where to run this code : should I create a php file with this code with loading any modx lib ?
Or create a snippet ?

In what way? What are you hoping to do via CLI on installs? Is it something that could go into the setup XML https://docs.modx.com/current/en/getting-started/installation/cli/config.xml?

Using a snippet would probably be more secure, but you won’t be able to create the snippet and a page to run it on until after MODX is installed.

If what you want to do is the same each time, you could create a transport package locally with MyComponent, copy it to the core/packages directory, and everything you want would be done when the package is installed, though MyComponent might be overkill for what you want to do.

You’ll also need to (if you aren’t already) refresh the cache after changing a setting that way, see: https://docs.modx.com/current/en/extending-modx/caching#refreshing-the-modx-core-cache