Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » New Config Recursion Issues

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 412 Views
  • tazzydemon
    Avatar
    Community Member
    120 Posts

    New Config Recursion Issues Link to this post

    The Config.php class and assiciated Object functions have changed significantly in 3.0.2+ and I have been working with the Multilingual Module which parses the config (old statics) of all the other modules in order to add its own fields.

    It does this in its get_extra_config() function and in this function itself calls Config:inst->get(), which in turn via Object.php calls the very same get_extra_config. 200 loops later, php crashes.

    I have posted about this loop in Trac and the lack of a function that does not recurse. I have been tearing my hair out over this one and though I had fixed it by throwing away the first result field from loop over the $subclasses result from line

    $subclasses = ClassInfo::subclassesFor($decoratedclass);

    This breaks the loop but results in the decoratedclass never having fields added.

    How the devil to I get the old (well, new) statics for a class without using the Config::inst()-> get() which would appear to be useless in this case. Any ideas? I am assuming this addition of db fields has to be done in the get_extra_config function or could it be done somewhere else.

    I can't use DataObject::custom_database_fields($class) or database_fields($class) as they call Config::inst()->get() too. Attempts to recreate an object Using DataObject::create_from_string($class); still exhibit the same problem

    I am reluctantly forced to conclude that at present its impossoble to do. Here is a sample with the broken Config::inst()->get as the method

    $origfields = Config::inst()->get($class, 'db', Config::FIRST_SET);

    if (is_array($origfields)) {
    $origkeys = array_keys($origfields);
    if (!($class_multilingual_fields = Config::inst()->get($class, 'multilingual_fields', Config::FIRST_SET))) {
    $class_multilingual_fields = array();
    }
    foreach (Multilingual::multilingual_extra_langs() as $lang) {
    //look after the global fields set in _config.php
    foreach ($global_multilingual_fields as $fieldtotranslate) {
    // make sure we find a field with correct name that hasnt been translated before
    if (in_array($fieldtotranslate, $origkeys) && !in_array($fieldtotranslate . "_" . $lang, $origfields)) {
    $db[$fieldtotranslate . "_" . $lang] = $origfields[$fieldtotranslate];
    }
    }
    //look after class multilingual fields set on class
    foreach ($class_multilingual_fields as $fieldtotranslate) {
    // make sure we find a field with correct name that hasnt been translated before
    if (in_array($fieldtotranslate, $origkeys) && !in_array($fieldtotranslate . "_" . $lang, $origfields)) {
    $db[$fieldtotranslate . "_" . $lang] = $origfields[$fieldtotranslate];
    }
    }
    }

    if (sizeof($db) > 0) {
    Config::inst()->update($class, 'db', array_merge($origfields, $db));
    }


    Julian

  • tazzydemon
    Avatar
    Community Member
    120 Posts

    Re: New Config Recursion Issues Link to this post

    Thanks to Hamish for putting me back on a track I had derailed myself from. One can break the loop with the config constants.

    Fixed in this case use:

    Config::inst()->get($class, 'db', Config::EXCLUDE_EXTRA_SOURCES | Config::UNINHERITED)

    To not get extras and not pull in the config from parents (in this case SiteTree?)

    Julian

    412 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.