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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Add Image to CSV import through ModelAdmin?


Go to End


6 Posts   1679 Views

Avatar
Pix

Community Member, 158 Posts

21 August 2014 at 5:38am

Can anyone explain the process for extending the ModelAdmin CSV to include an image? I'm OK with uploading the images first to the assets directory, but I don't know how to establish the has_one relationship through the CSV import. I've read through:

http://doc.silverstripe.org/framework/en/howto/csv-import

but I don't see anything dealing with images that are already uploaded....

Thanks!

Avatar
Pix

Community Member, 158 Posts

25 August 2014 at 1:49pm

Anyone know how to do this?

Avatar
mi3ll

Community Member, 24 Posts

25 August 2014 at 2:54pm

Hello Pix!

You will have to implement the $relationCallbacks array (as seen in the last example on the http://doc.silverstripe.org/framework/en/howto/csv-import image)

Your code would probably be something like this:

class MyCsvBulkLoader extends CsvBulkLoader {
   public $columnMap = array(
      // Other column map titles
      'Image' => 'Image.Filename', 
   );

   public $relationCallbacks = array(
      'Image' => array(
         'relationname' => 'Image',
         'callback' => 'getImageByFilename'
      )
   );
  
  public function getImageByFilename(&$obj, $val, $record) {
        // sanitise filename
        $filename = trim(strtolower(Convert::raw2sql($val)));
        $filenamedashes = str_replace(" ", "-", $filename);
	
        // Find one image with the filename
        if($filename && $image = DataObject::get_one('Image', "LOWER(\"Filename\") LIKE '%$filename%' OR LOWER(\"Filename\") LIKE '%$filenamedashes%'")){ //ignore case
             if($image instanceof Image && $image->isInDB()){
                    $image->write();
                    return $image;
             }
         }
	 
         return null;
   }
}

(This code was copied from https://github.com/burnbright/silverstripe-shop/blob/master/code/cms/ProductBulkLoader.php, so thanks to @burnbright for doing all the work)

Avatar
Pix

Community Member, 158 Posts

25 August 2014 at 5:39pm

Hello mi3ll!

Fantastic! This is really helpful, thank you very very much. Going to give it a go!

Avatar
Pix

Community Member, 158 Posts

4 September 2014 at 6:21am

So close, so close. I can do a CSV import (after uploading images to assets/Uploads), but then what happens is the Image Object seems to get messed up...the Filename loses it's path to assets/Uploads and it doesn't even show in the Files admin any more. So it's working, but not really. This is my code:


<?php

class PhotoBulkLoader extends CsvBulkLoader { 

	public $columnMap = array( 
	// Other column map titles 
		'Title' => 'Title',
		'Tags' => 'Tags',
		'MainCategory' => 'MainCategory',
		'Artist' => 'Artist',
		'Medium' => 'Medium',
		'Image' =>'Image.Filename'
	);

	public $relationCallbacks = array( 
		'Image.Filename' => array( 
			'relationname' => 'Image', 
			'callback' => 'getImageByFilename' 
		) 
	);

	public function getImageByFilename(&$obj, $val, $record) { 
		// sanitise filename 
		$filename = trim(strtolower(Convert::raw2sql($val))); 
		$filenamedashes = str_replace(" ", "-", $filename); 
	
		// Find one image with the filename 
		if($filename && $image = DataObject::get_one('Image', "LOWER(\"Filename\") LIKE '%$filename%' OR LOWER(\"Filename\") LIKE '%$filenamedashes%'")){ //ignore case 
			if($image instanceof Image && $image->isInDB()){
				$image->write();
				return $image; 
			} 
		} 
		return null; 
	} 

}

Attached Files
Avatar
Pix

Community Member, 158 Posts

4 September 2014 at 8:20am

Edited: 04/09/2014 8:21am

Well, I got this working, not sure if it's the best answer, but what I wound up doing was changing my CSV to include the image path as part of the import as so:

Image.Filename, Title, Tags, MainCategory, Artist, Medium
assets/Uploads/greyhound.jpg, Grehound, adorable, Animals, Me, photo