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

Dailytask import with images


Go to End


3 Posts   2047 Views

Avatar
aarono

Community Member, 34 Posts

23 February 2011 at 10:51am

Edited: 23/02/2011 10:52am

Hi guys

I need to have a Dailyimport from CSV to a DataObject. I have got it reading the CSV and putting together then writing the DataObject but now I need it to also read a folder of images and import those into the DataObject has_many.

So far I have loaded all the images into an array, and checked them against the import. So is there a function I can use when looping over the images to import them into the assets/ folder and load them against the DataObject? The images will already be in a folder on the host.

Here are my objects:

class VehiclePreowned extends DataObject {

	public static $db = array (
		'UniqeNo' => 'Varchar(255)',
		'StockNo' => 'Varchar(255)',
		'Year' => 'Varchar(255)',
		'Make' => 'Varchar(255)',
		'Model' => 'Varchar(255)',
		'Desc' => 'Varchar(255)',
		'BodyStyle' => 'Varchar(255)',
		'Doors' => 'INT',
		'ChassisNo' => 'Varchar(255)',
		'CC' => 'INT',
		'FuelType' => 'Enum("Petrol,Diesel")',
		'KM' => 'INT',
		'ExtColour' => 'Varchar(255)',
		'IntColour' => 'Varchar(255)',
		'Comments' => 'HTMLText'
	);

	public static $has_one = array (
		'Folder' => 'Folder'
	);
	
	public static $has_many = array (
		'VehicleImages' => 'VehiclePreownedImage'
	);
	
	static $searchable_fields = array(
		'Make',
		'Model'
	);
	static $summary_fields = array(
		'Make',
		'Model'
	);
	
	function canCreate() {return true;}
   	function canEdit() {return true;}
   	function canDelete() {return true;}
	function canPublish() {return true;}


	public function getCMSFields() {
		$fields = parent::getCMSFields();
		
		//$fields->addFieldToTab("Root.Main",new TextField('Name','Name'));
		
		return $fields;
	}

	function onBeforeWrite() {
		parent::onBeforeWrite();
		if(isset($_POST['Name'])) {
		  $clean_name = SiteTree::generateURLSegment($_POST['Name']);
			if($this->FolderID) {
				$this->Folder()->setName($clean_name);
				$this->Folder()->Title = $clean_name;

				$this->Folder()->write();
			}
			else {
				$folder = Folder::findOrMake($clean_name);
				$this->FolderID = $folder->ID;
			}
		}
	}

}

Image DataObject and Image

class VehiclePreownedImage extends DataObject {

	public static $db = array (
	);
	static $has_one = array(
		'VehiclePreowned' => 'VehiclePreowned',
		'VehicleImageFile' => 'VehiclePreownedImage_Image'
   	);
	function canCreate() {return true;}
   	function canEdit() {return true;}
   	function canDelete() {return true;}
	function canPublish() {return true;}

}
class VehiclePreownedImage_Image extends Image {
 
   function generateVehicleImageListing($gd) {
      return $gd->croppedResize(191,161);
   }
   function generateVehicleImageThumb($gd) {
      return $gd->croppedResize(74,63);
   }
   function generateVehicleImageMain($gd) {
      return $gd->croppedResize(383,325);
   }
   function generateVehicleImageLarge($gd) {
      return $gd->croppedResize(700,600);
   }
}

Any help is much appreciated

Cheers!!

Avatar
Willr

Forum Moderator, 5523 Posts

23 February 2011 at 2:16pm

So is there a function I can use when looping over the images to import them into the assets/ folder and load them against the DataObject?

For loading them into assets I'm not use of the easiest /best way of doing that, most of the loadIntoFile() functionality assumes it is a file upload rather than moving a file. First I guess you should move the file to assets (using PHP exec() or how ever you want to move the file) then either

a) run Filesystem::sync(). This will create all the image dataobjects for you.

b) create the dataobjects manually

foreach($images as $image) {
$img = new Image();
$img->Filename = $image['Filename'];
$img->write();

To attach the image to a DO then it's a little easier

$do->VehicleImages()->add($img);

Avatar
aarono

Community Member, 34 Posts

24 February 2011 at 10:39am

Thats prefect thanks Willr. I went with creating the dataobjects manually