Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [Solved] Inherited relationCallbacks

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

Page: 1
Go to End
Author Topic: 350 Views
  • nbenn
    Avatar
    Community Member
    3 Posts

    [Solved] Inherited relationCallbacks Link to this post

    Dear Forum

    I am trying to CSV import data for classes (Laptop, Tablet) that extend my class Device (which in turn is a child of DataObject). Device has a has_one relationship to the class Manufacturer (extending Page) and Manufacturer a has_many to Device. My setup is also shown in the attached diagram.

    class Device extends DataObject {

       static $db = array (
          'Dimensions' => 'Varchar',
          'Weight' => 'Varchar',
       );

       static $has_one = array (
          'Manufacturer' => 'Manufacturer'
       );
    }

    class Laptop extends Device {
       
       static $db = array (
          'VideoCard' => 'Varchar',
          'HDCapacity' => 'Varchar'
       );
    }

    class Manufacturer extends Page {

       static $has_many = array(
          'Devices' => 'Device'
       );
    }

    I set up a class extending ModelAdmin as follows

    class DeviceAdmin extends ModelAdmin {

       public static $managed_models = array(
          'Laptop',
          'Manufacturer'
       );

       static $model_importers = array(
          'Laptop' => 'LaptopCsvBulkLoader',
       );
    }

    LaptopCsvBulkLoader extends DeviceCsvBulkLoader, which extends CsvBulkLoader.

    class DeviceCsvBulkLoader extends CsvBulkLoader {

       public $columnMap = array(
          'Dimensions' => 'Dimensions',
          'Weight' => 'Weight',
          'Manufacturer' => 'Manufacturer.Title'
       );

       public $relationCallbacks = array(
          
          'Manufacturer.Title' => array(
             'relationname' => 'Manufacturer',
             'callback' => 'getManufacturerByTitle'
          )
       );

       public static function getManufacturerByTitle(&$obj, $val, $record) {

          return Manufacturer::get()->filter('Title', $val)->First();
       }
    }

    class LaptopCsvBulkLoader extends DeviceCsvBulkLoader {

       public $columnMap = array(
          'VideoCard' => 'VideoCard',
          'HDCapacity' => 'HDCapacity'
       );
    }

    When I import a CSV with this code in place, everything works as I would expect, except for the relation callbacks. Neither are new Manufacturers generated when necessary, nor are existing ones linked to newly imported devices. The linking of Devices and Manufactures can be done manually through the admin backend though.

    How do I have to modify my code for this to work ? Or is what I'm trying to do not supported by the provided relationCallbacks functionality?

    Any suggestions are highly appreciated.

    Best regards,
    Nicolas

    Attached Files
  • nbenn
    Avatar
    Community Member
    3 Posts

    Re: [Solved] Inherited relationCallbacks Link to this post

    Problem solved. It was a stupid mistake on my side: of course, I am overwriting the columnMap array of DeviceCsvBulkLoader in its children thereby masking the relationCallbacks trigger...

    350 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.