Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

New Config Recursion Issues


Reply


2 Posts   466 Views

Avatar
tazzydemon

Community Member, 122 Posts

11 September 2012 at 11:13am

Edited: 11/09/2012 2:13pm

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

Avatar
tazzydemon

Community Member, 122 Posts

11 September 2012 at 4:29pm

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