News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

VmConfig::set() not updating config in db

Started by vmfyelloq19, December 13, 2023, 21:50:21 PM

Previous topic - Next topic

vmfyelloq19

Hello,

in a custom admin module I am trying to set catalog mode like this:

Quote
if( ! class_exists('VmConfig')) require JPATH_ADMINISTRATOR . '/components/com_virtuemart/helpers/config.php';
VmConfig::loadConfig();

echo VmConfig::get('use_as_catalog'); # -> 0
VmConfig::set('use_as_catalog',1)
echo VmConfig::get('use_as_catalog'); # -> 1

This looked fine at first but use_as_catalog is not written to #__virtuemart_configs. I guess I'm missing something trivial here. (Logged in as superadmin, if that matters).
Any hints?

# VirtueMart 3.8.8 10472

Studio 42

Your code only set the value in the memory not in the real virtuemart config
Only here JOOMLAROOT/administrator/components/com_virtuemart/controllers/config.php
all values are saved in the table

vmfyelloq19

Hello Studio 42

thank you for the reply. How can I trigger saving the config? I guess there is an API call for thsi that I am missing?

Studio 42

No api call for this. You have to write your own code
But you can set it using a  Joomla system plugin for eg

vmfyelloq19

#4
Hello Studio 42,

so my code would basically be a copy of VirtuemartControllerConfig save()?
Would instead creating an instance of VirtuemartControllerConfig and use save() from there have any side effects?

Update:
No, it looks like you cannot use the save() method for anything. You can pass $data but that get's overwritten by request/post right away:

class VirtuemartControllerConfig extends VmController {
    // ...
    function save($data = 0){
        vRequest::vmCheckToken();
        $model = VmModel::getModel('config');
        $data = vRequest::getPost();

Jumbo!

You can use the following codes to save configuration data to the database.

<?php

// Your configuration data
$data = array(
'use_as_catalog' => 0
);

// Register VM Config Class
JLoader::register('VmConfig'JPATH_ADMINISTRATOR '/components/com_virtuemart/helpers/config.php');

// Load config
VmConfig::loadConfig();

// Get an instance of the Config model
$model VmModel::getModel('config');

// If save fails
if (!$model->store($data)) {
// Do your stuff if save fails
return false;
}

// Reload the newly saved config
VmConfig::loadConfig();

vmfyelloq19

#6
Hello Jumbo,

thank you very much!
I was just investigating if I'd need a full data set or just the required keys.
Works perfectly!

vmfyelloq19

#7
One issue remains:
Somhow the virtuemart safe path (forSale_path) gets removed from the config.

UPDATE
Looks like the config model store($data) method completely ignores all data stored in the DB and only loads config from config file.


  function store(&$data) {
      // ...
      //We create a fresh config
        $config = VmConfig::loadConfig(false,true);

        //We load the config file
        $_raw = self::readConfigFile();
        $_value = join('|', $_raw);
        //We set the config file values as parameters into the config
        $config->setParams($_value);

        //We merge the array from the file with the array from the form
        //in case it the form has the same key as the file, the value is taken from the form
        $config->_params = array_merge($config->_params,$data);

Ghost


vmfyelloq19

#9
Here's my updated code that gets the full config data from database first:

// Get current full config from database
$VmConfig = VmConfig::loadConfig(); // VmConfig object
                                    // $VmConfig->get(key)
                                    // $VmConfig->set(key,value)
                                    // $VmConfig->_raw [all data as string, |-delimited]
                                    // VmConfig->_params [all data as array]
$VmConfigData = $VmConfig->_params;

// Update config data with custom values:
$VmConfigData['use_as_catalog'] = 1;

// Store into database:
$model = VmModel::getModel('config');
if( ! $model->store($VmConfigData)) {
    JFactory::getApplication()->enqueueMessage('failed');
    return false;
}   
VmConfig::loadConfig();

vmfyelloq19

Hello Ghost,

> Have you tried VmConfig::updateDbEntry() ?

Did not know this method. It's not in the calss documentation, is it? Will try!