Jump to content

How can I set/get module-data from DB when not implementing ConfigurableModule?


horst
 Share

Recommended Posts

Hi, I create a little module that extends Process and creates a page in backend under setup.

I need to store and retrieve a few (config)data for it, but don't want to make it a ConfigurableModule because than I have redundant places where I can setup config-data , I guess.

How can I store / retrieve some data into the place where modules store it by default, but without implementing ConfigurableModule?

Edited by horst
Link to comment
Share on other sites

How about storing it on a page you put somewhere? I dont think it possible to use module config without using the interface.

Otherwise you could maybe store it manually to db on the module using json encode and sql.

Link to comment
Share on other sites

How about storing it on a page you put somewhere? ...

...

Hi Soma, that's perfect for this needs, as I allready create a page for the module, I now also create a childpage for the config data.

I have to learn to think less complicated when working with PW!

I also have created 2 permissions and 2 roles to have access control. It tooks only 5 minutes for this!  (Das ist so geil!)

If I have not allready said it: I love ProcessWire! :wub:

  • Like 1
Link to comment
Share on other sites

Go ahead and make your module implement ConfigurableModule, even if you don't want any interactive configuration:

class MyModule extends WireData implements Module, ConfigurableModule { 

Add a getModuleConfigInputfields function to your class that just does this:

public function getModuleConfigInputfields(array $data) {
  return new InputfieldWrapper();
}

For data that you want to store, call upon the $modules API var like this:

$data = array('foo' => 'hello', 'bar' => 'goodbye'); 
wire('modules')->saveModuleConfigData($this, $data); 

For data that you want to retrieve: 

$data = wire('modules')->getModuleConfigData($this); 

Your config data will be populated to your module automatically after __construct() but before init(). As a result, you might want to set default values in your __construct(): 

public function __construct() {
  $this->set('foo', 'hi'); 
  $this->set('bar', 'bye'); 
}

public function init() {
  // $this->foo is now 'hello' (not 'hi')
  // $this->bar is now 'goodbye' (not 'bye')
}

Using this method, your module can always access it's config data directly via $this->foo and $this->bar. Meaning, you probably won't ever need to use the getModuleConfigData() function, but it's good to know its there. However, you will need to use the saveModuleConfigData() function since you are saving configuration data non-interactively. 

  • Like 4
Link to comment
Share on other sites

Go ahead and make your module implement ConfigurableModule, even if you don't want any interactive configuration:

class MyModule extends WireData implements Module, ConfigurableModule { 

Can I also use it with extending Process instead of WireData?

class MyModule extends Process implements Module, ConfigurableModule {

...

Using this method, your module can always access it's config data directly via $this->foo and $this->bar. Meaning, you probably won't ever need to use the getModuleConfigData() function, but it's good to know its there. However, you will need to use the saveModuleConfigData() function since you are saving configuration data non-interactively. 

That's awesome! I will change that after finishing some other parts :-)

Is there a limit on size to store? For example can I store a serialized array of 5MB or are any bads with that ?

Many thanks!

Link to comment
Share on other sites

the config "data" field in db is a "TEXT" field so about ~64kb. Module config is stored as JSON and to store 5mb of data you would need a MEDIUMTEXT field ~16mb.

Of course you can, but depending on your data and for what it's used it's maybe not a good strategie.

To add another possibility, you could add a textarea (MEDIUMTEXT) field to the "admin" template, and store the data there. But if you would need flexible and larger data stored I would certainly also consider using pages.

  • Like 1
Link to comment
Share on other sites

Hi Soma, I'm actually working on scanning filesystem and parse 5000 files for metadata. As I run into errors (e.g. not knowing to use $sanitizer->selectorValue()) I don't want to wait 3 minutes every loop, - so store a serialized array in cache-file (site/assets/cache/).

I've asked to get a sense for the possibilities. Actually I only will use the ModuleConfigData-storage for some Path- and Extension-strings.

Also look at core modules if you arent sure about something. Look up ProcessPageList.module which is configurable.

 Thanks, it only implements ConfigurableModule, so it must have Module allready.

(will look into that closer) :-)

EDIT: have found this Post: http://processwire.com/talk/topic/1313-modules-process-and-the-difference/#entry11738

Edited by horst
Link to comment
Share on other sites

For storing that scale of data, I think you'd want to have your module create it's own DB table(s) to store it in. Several modules do this. They create their tables via the __install() method, and drop them via the __uninstall() method. 

Link to comment
Share on other sites

For storing that scale of data, I think you'd want to have your module create it's own DB table(s) to store it in. Several modules do this. They create their tables via the __install() method, and drop them via the __uninstall() method.

Thanks Ryan, - but I don't need it now with this module. It was just a to quick asked question. Anyway it's good to know for the future. :)

I will go with the ConfigurableModule.

... I'm actually working on scanning filesystem and parse 5000 files for metadata. As I run into LOTS of ERRORS (e.g. not knowing to use $sanitizer->selectorValue()) I don't want to wait 3 minutes every loop, - so store a serialized array in cache-file (site/assets/cache/).

I've only asked to get a sense for the possibilities. Actually I only will use the ModuleConfigData-storage for some Path- and Extension-strings.

But if one ask here on the forum it maybe that one get to much information for one's brain to handle / process / assimilate. :grin:

But, just keep on with it guys!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...