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

how to get image object?


Go to End


6 Posts   3691 Views

Avatar
dacar

Community Member, 173 Posts

20 September 2013 at 10:09pm

Edited: 20/09/2013 11:14pm

Hi,
i am trying to get and manipulate an image object (SS3.1) within a function.

But $image = Image::get()->where("\"Name\" LIKE '%$filename%'"); wii return a DataList and not the image object itself.
Can anybody give a hint? Here is the complete function:

public static function imageByFilename(&$obj, $val, $record){
$filename = strtolower(Convert::raw2sql($val));
$image = Image::get()->where("LOWER(\"Name\") LIKE '%$filename%'");
if($filename && $image){
if($image->exists()){
echo "IMAGE: ";echo "<pre>".var_dump($image)."</pre>"; die();
$image->ClassName = self::get_product_class_name().'_Image';
$image->write();
return $image;
}
}
return null;
}

Greetings from germany, Carsten.

EDIT:

I have inserted a foreach loop and now i get the image object. But i still get a server error at $image->write(); in imageByFilename().

What i want to do is: 1) write the ProduktbildID from my has_one relation to the Produkte.ProduktbildID field and 2.) alter the ClassName in File from Image to Produkte_Image.

Here is some code from my "class ProdukteCsvBulkLoader extends CsvBulkLoader"

   public $relationCallbacks = array(
	  'File.Name' => array(
         'relationname' => 'Image',
         'callback' => 'imageByFilename'
      )
   );

	public static function imageByFilename(&$obj, $val, $record){
		$filename = strtolower(Convert::raw2sql($val));
		$images = Image::get()->where("LOWER(\"Name\") LIKE '%$filename%'");
		
		foreach($images AS $imageout) {
			$image = $imageout;
		}
		
		if($filename && $image){ 
			if($image->exists()){
				$image->ClassName = self::get_product_class_name().'_Image'; 
				$image->write();
				return $image;
			}
		}
		return null;
	}

Avatar
dacar

Community Member, 173 Posts

21 September 2013 at 2:21am

i am about to solve the problem. public $relationCallbacks calls public static function imageByFilename.
Everything works fine, only the last return will cuase an server error: return $image. $image->write(); works fine, but return $image; fails.

Here is the complete function:

public static function imageByFilename(&$obj, $val, $record){
$filename = strtolower(Convert::raw2sql($val));
if($filename && $image = DataObject::get_one('Image',"LOWER(\"Name\") LIKE '%$filename%'")){

if($image->ID){
$image->ClassName = 'Produkte_Image';
$image->write();
return $image;
}
}
return null;
}

Can anybody help?

Avatar
dacar

Community Member, 173 Posts

25 September 2013 at 9:54pm

Hi, unfortunately i still get an server error. The relation CallBack for 'Marke' works fine. But what is the secret behind relationCallbacks and Images? The Object is there! Please can anybody help...

Here is the code i use:

public $relationCallbacks = array(
'Marke.Title' => array(
'relationname' => 'Marke',
'callback' => 'getMarkeByTitle'
),
'Image.Name' => array(
'relationname' => 'Produktbild_Image',
'callback' => 'imageByName'
)
);

public static function imageByName($obj, $val, $record){
$filename = strtolower(Convert::raw2sql($val));

if($filename && $image = DataObject::get_one('Image',"LOWER(\"Name\") LIKE LOWER('%$filename%')")){
if($image->ID){
$image->ClassName = 'Produktbild_Image';
$image->write();
$image = DataObject::get_one('Produktbild_Image',"LOWER(\"Name\") LIKE LOWER('%$filename%')");
$image = Produktbild_Image::get()->filter('Name', $filename)->First();
/* */
echo "<pre>";
var_dump($image);
echo "</pre>";

return $image;
}
}
return null;
}

The var_dump() returns the following:

object(Produktbild_Image)#1161 (17) {
["destroyed"]=>
bool(false)
["model":protected]=>
object(DataModel)#1029 (1) {
["customDataLists":protected]=>
array(0) {
}
}
["record":protected]=>
array(11) {
["ClassName"]=>
string(17) "Produktbild_Image"
["Created"]=>
string(19) "2013-09-19 16:39:43"
["LastEdited"]=>
string(19) "2013-09-25 11:24:44"
["Name"]=>
string(20) "ACE-08631088-G29.JPG"
["Title"]=>
string(16) "ACE 08631088 G29"
["Filename"]=>
string(41) "assets/Produktbilder/ACE-08631088-G29.JPG"
["ShowInSearch"]=>
string(1) "1"
["ParentID"]=>
string(1) "7"
["OwnerID"]=>
string(1) "1"
["ID"]=>
int(24)
["RecordClassName"]=>
string(17) "Produktbild_Image"
}
["changed":"DataObject":private]=>
array(0) {
}
["original":protected]=>
array(11) {
["ClassName"]=>
string(17) "Produktbild_Image"
["Created"]=>
string(19) "2013-09-19 16:39:43"
["LastEdited"]=>
string(19) "2013-09-25 11:24:44"
["Name"]=>
string(20) "ACE-08631088-G29.JPG"
["Title"]=>
string(16) "ACE 08631088 G29"
["Filename"]=>
string(41) "assets/Produktbilder/ACE-08631088-G29.JPG"
["ShowInSearch"]=>
string(1) "1"
["ParentID"]=>
string(1) "7"
["OwnerID"]=>
string(1) "1"
["ID"]=>
int(24)
["RecordClassName"]=>
string(17) "Produktbild_Image"
}
["brokenOnDelete":protected]=>
bool(false)
["brokenOnWrite":protected]=>
bool(false)
["components":protected]=>
NULL
["unsavedRelations":protected]=>
NULL
["sourceQueryParams":protected]=>
NULL
["failover":protected]=>
NULL
["customisedObject":protected]=>
NULL
["objCache":"ViewableData":private]=>
array(0) {
}
["class"]=>
string(17) "Produktbild_Image"
["extension_instances":protected]=>
array(3) {
["FulltextSearchable"]=>
object(FulltextSearchable)#1162 (5) {
["searchFields":protected]=>
string(28) ""Filename","Title","Content""
["owner":protected]=>
NULL
["ownerBaseClass":protected]=>
string(4) "File"
["ownerRefs":"Extension":private]=>
int(0)
["class"]=>
string(18) "FulltextSearchable"
}
["SiteTreeFileExtension"]=>
object(SiteTreeFileExtension)#1163 (4) {
["owner":protected]=>
NULL
["ownerBaseClass":protected]=>
string(4) "File"
["ownerRefs":"Extension":private]=>
int(0)
["class"]=>
string(21) "SiteTreeFileExtension"
}
["Hierarchy"]=>
object(Hierarchy)#1164 (7) {
["markedNodes":protected]=>
NULL
["markingFilter":protected]=>
NULL
["_cache_numChildren":protected]=>
NULL
["owner":protected]=>
NULL
["ownerBaseClass":protected]=>
string(4) "File"
["ownerRefs":"Extension":private]=>
int(0)
["class"]=>
string(9) "Hierarchy"
}
}
["beforeExtendCallbacks":protected]=>
array(0) {
}
["afterExtendCallbacks":protected]=>
array(0) {
}
}

Avatar
dacar

Community Member, 173 Posts

26 September 2013 at 4:32am

Hi,

Now i have an errorlog and it says:

[Error] Uncaught Exception: Object->__call(): the method 'produktbild_image' does not exist on 'Produkte'

But in Produkte.php the relation is set via:

static $has_one = array (
'Marke' => 'Marke',
'Produktbild' => 'Produktbild_Image'
);

Can anybody see what is missing?

• Object->__call(Produktbild_Image,Array)
DataObject.php:804
• Produkte->Produktbild_Image()
DataObject.php:804
• DataObject->update(Array)
CsvBulkLoader.php:169
• CsvBulkLoader->processRecord(Array,Array,BulkLoader_Result,)
CsvBulkLoader.php:81
• CsvBulkLoader->processAll(/var/tmp/phpcahOoP)
BulkLoader.php:146
• BulkLoader->load(/var/tmp/phpcahOoP)
ModelAdmin.php:430
• ModelAdmin->import(Array,Form,SS_HTTPRequest)
Form.php:358
• Form->httpSubmission(SS_HTTPRequest)
RequestHandler.php:288
• RequestHandler->handleAction(SS_HTTPRequest,httpSubmission)
RequestHandler.php:200
• RequestHandler->handleRequest(SS_HTTPRequest,DataModel)
RequestHandler.php:222
• RequestHandler->handleRequest(SS_HTTPRequest,DataModel)
Controller.php:153
• Controller->handleRequest(SS_HTTPRequest,DataModel)
LeftAndMain.php:438
• LeftAndMain->handleRequest(SS_HTTPRequest,DataModel)
AdminRootController.php:89
• AdminRootController->handleRequest(SS_HTTPRequest,DataModel)
Director.php:325
• Director::handleRequest(SS_HTTPRequest,Session,DataModel)
Director.php:143
• Director::direct(/admin/produkte/Produkte/ImportForm,DataModel)
main.php:128
Details
$_SERVER['HTTP_ACCEPT'] text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
$_SERVER['HTTP_ACCEPT_CHARSET']
$_SERVER['HTTP_ACCEPT_ENCODING'] gzip, deflate
$_SERVER['HTTP_ACCEPT_LANGUAGE'] de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
$_SERVER['HTTP_REFERRER']
$_SERVER['HTTP_USER_AGENT'] Mozilla/5.0 (Windows NT 6.1; rv:23.0) Gecko/20100101 Firefox/23.0
$_SERVER['HTTPS']
$_SERVER['REMOTE_ADDR'] 85.8.108.115

Avatar
dacar

Community Member, 173 Posts

26 September 2013 at 5:30am

Hi,

i have changed the code to the following and everything is written to the database. The Server error has gone.

public $columnMap = array(
'Artikelnr. Zusammengefasst' => 'Produktbild.Name'
);

public $relationCallbacks = array(
'Produktbild.Name' => array(
'relationname' => 'Image',
'callback' => 'imageByName'
)
);

public static function imageByName($obj, $val, $record){
$filename = strtolower(Convert::raw2sql($val));
if($filename && $image = DataObject::get_one('Image',"\"Name\" LIKE '%$filename%'")){
if($image->ID){
$image->ClassName = 'Produktbild_Image';
$image->write();
return $image;
}
}
return null;
}

But now i get an validation error, but the filetype "jpg" is allowed. And the files are .jpg

Validation error: Validation error writing a Produktbild_Image object: Dateityp ist nicht erlaubt (erlaubt sind: , ace, arc, arj, asf, au, avi, bmp, bz2, cab, cda, css, csv, dmg, doc,
docx, flv, gif, gpx, gz, hqx, htm, html, ico, jar, jpeg, jpeg, jpg, jpg,
js, kml, m4a, m4v, mid, midi, mkv, mov, mp3, mp4, mpa, mpeg, mpg, ogg, ogv,
pages, pcx, pdf, pkg, png, pps, ppt, pptx, ra, ram, rm, rtf, sit, sitx,
swf, tar, tgz, tif, tiff, txt, wav, webm, wma, wmv, xhtml, xls, xlsx, xml,
zip, zipx). Object not written.

Can anybody help?

Avatar
dacar

Community Member, 173 Posts

11 October 2013 at 12:07am

writing the imageid to the database has been solved. But now i have problems with copying the image files. Ther went something wrong with Filename and Name.

but there seems to be a problem with copying the files: [Error] Uncaught Exception: Cannot move /home/www/22/42/cwwebde123/art-/assets/new-produktbild-image to /home/www/22/42/cwwebde123/art-/assets/NEW-HAVEN-08578078-G55.jpg - /home/www/22/42/cwwebde123/art-/assets/new-produktbild-image doesn't exist

/home/www/22/42/cwwebde123/art-/framework/filesystem/File.php:532

<?php
class ProdukteCsvBulkLoader extends CsvBulkLoader {
public $columnMap = array(
'Marke'     =>  'Marke.Title',
'Leisten'       =>  'Leisten',
'Artikelnr'     =>  'Artikelnummer',
'Artikelname'   =>  'Artikelname',
'Saisoncode'    =>  'Saison',   
'Saionname'     =>  'Saionname',    
'Geschlechtcode'    =>  'Geschlecht',
'Geschlechtname'    =>  'Geschlechtname',
'Farbcode'      =>  'Farbcode',
'Farbname'      =>  'Farbname',
'Artikelnrzusammengefasst'      =>  'Artikelnummerzusammengefasst',
'Beschreibungdeutsch'           =>  'BeschreibungDE',
'Beschreibungenglisch'          =>  'BeschreibungEN',
'Artikelnrzusammengefasst'      =>  'Produktbild.Name'
);


public $duplicateChecks = array(
'Artikelnr' => 'Artikelnummer'
);

public $relationCallbacks = array(
'Marke.Title' => array(
'relationname' => 'Marke',
'callback' => 'getMarkeByTitle'
),
'Produktbild.Name' => array(
'relationname' => 'Produktbild',
'callback' => 'imageByName'
)
);


public static function getMarkeByTitle($obj, $val, $record) {
$marke = Marke::get()->filter('Title', $val)->First();
return $marke;
}

static function imageByName($obj, $val, $record){
$filename = Convert::raw2sql($val);
$filename = "assets/Produktbilder/".$val;
if($filename && $image = DataObject::get_one('Image',"\"Filename\" LIKE '%$filename%'")){ 
if($image->ID && $image->ClassName = 'Image'){
$image->ClassName = 'Produktbild_Image';
$image->Filename = $filename;
echo $image->Filename; die();
return $image; 
}
}
return null;
}

}
?>

Can anybody help?