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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

[Solved] Inherited relationCallbacks


Go to End


2 Posts   1412 Views

Avatar
nbenn

Community Member, 3 Posts

8 June 2013 at 8:03pm

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
Avatar
nbenn

Community Member, 3 Posts

8 June 2013 at 10:56pm

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