2

I'm looking to learn more about the internals of Magento!

I recently used Inchoo's tutorial to allow administrators to set a crontab schedule from System > Configuration. See it here. In it, I noticed that a setting which is usually set in config.xml is being set in the database table core_config_data instead.

Normal config.xml method:

<crontab>
    <jobs>
        <my_custom_crontab>
            <schedule><cron_expr>* * * * *</cron_expr></schedule>
            <run><model>mymodule/observer::hookMethod</model></run>
        </my_custom_crontab>
    </jobs>
</crontab>

Alternate Database Record:

scope   scope_id    path                                                value
default 0           crontab/jobs/my_custom_crontab/schedule/cron_expr   * * * * *

Can anyone tell me

  • What config settings can be located in the database, or if all of them can be.
  • Where Magento's logic (code) to pull these values from the database lives.
  • What the hierarchy looks like / what is given priority.

One side effect I've noticed from storing these config.xml settings in the database is that they suddenly have scope. (default, website, store, store view.) I'm not aware of replicating such scope in the config.xml files, so I'm also wondering how that figures in.

Thanks!

7

In theory you can set any config value in the database (table core_config_data).
But for most of the cases, the values saved there have the path column like this: section/group/field. So only 3 parts. The cron schedule is an exception.

The general config of Magento comes from different sources. All of these sources are merged into one big xml. In case there are 2 nodes with the same xpath, every source will overwrite the previous source.
Here is the order of loading the config.

  1. All the xml files located in app/etc (no subfolders). This is loaded in Mage_Core_Model_App::_initBaseConfig().
  2. All the modules. Files located in app/etc/modules. This is done by Mage_Core_Model_Config::_loadDeclaredModules()
  3. The config.xml for all active modules loaded previously. Done in Mage_Core_Model_Config::loadModules()
  4. core_config_data table values. Done in Mage_Core_Model_Config::loadDb.

From all of these, magento creates one big config xml and smaller ones for each website and store view deriving from the main one.

And here is an example.
Let's say that you have this inside the config.xml of a module.

<default> <!-- config scope node -->
   <some_section>
       <some_group>
           <some_field>2</some_field>
       </some_group>
   </some_section>  
</default>

And you have in the db a line with the same path some_section/some_group/some_field and with the scope default, but the value is 1.
In the big config the final value will be 1 because the db config is loaded last.

A side note. When reading a value from the config, you cannot know if the value comes from any xml file or from the db and you shouldn't care.

|improve this answer|||||
  • Super duper!!!! – huzefam Aug 30 '14 at 16:50
  • 1
    epic! great teacher. pranaamam :) (folding my hands with respect, indian style). – Rajeev K Tomy Sep 19 '14 at 7:29
  • @Marius : this happends for each page request, right ? – Rajeev K Tomy Sep 19 '14 at 7:37
  • @programmer_rkt. yes and no. If you have the config cache enabled, the config is read from cache on the second request. Otherwise this happens for each page request. – Marius Sep 19 '14 at 7:57
  • @Marius : This point is absolutely woth for me. I was unaware of the order. Thanks for teaching me this ! – Rajeev K Tomy Sep 19 '14 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.