Custom CsvBulkLoader

bugs
silverstripe-4
Tags: #<Tag:0x00007f10c74cba38> #<Tag:0x00007f10c74cb538>

#1

Hello everyone,

I don’t understand if my problem is a bug or it’s my error.

I write a class that extends CsvBulkLoader, with variable $relationCallbacks like documentation

class LanguageIdsCsvBulkLoader extends CsvBulkLoader
{
    public $columnMap = [
        'Language' => 'Language.Code',
        'IDS' => '->importIds',
        'Ref' => null,
        'Translation' => 'StringValue'
    ];

    public $relationCallbacks = [
        'Language.Code' => [
            'relationname' => 'Language',
            'callback' => 'getLanguageByCode'
        ]
    ];

    public static function getLanguageByCode(&$obj, $val, $record)
    {
        return Language::get()->filter('Code', $val)->first();
    }
}

But when I import my csv file, the function getLanguageByCode is not called.

So, I use Debug::show() in class CsvBulkLoader, extaclty in processRecord function.

protected function processRecord($record, $columnMap, $results, $preview = false) {
    Debug::show($record);
    ...
}

If I debug $record variable I have an array like that:

[
    Language => 'en-en',
    IDS => 'high.IDS_TALL',
    StringValue => 'Tall'
]

In the first foreach of function processRecord(), the variable $fieldName has value:

  • Language (first loop)
  • IDS (second loop)
  • StringValue (third loop)

When try to check if isset $this->relationCallbacks[$fieldName], Language is not set, because it’s named ‘Language.Code’.

Then I edit my relationCallbacks array like this:

        public $relationCallbacks = [
            'Language' => [
                'relationname' => 'Language',
                'callback' => 'getLanguageByCode'
            ]
        ];

In this way I supposed that $this->relationCallbacks[$fieldName] (with $fieldName == ‘Language’) was set! But $fieldName, for some reason, is not equals to ‘Language’ (string), so $this->relationCallbacks[$fieldName] is not set!

Can someone help me to find what I’m wrong?