Set Attribute File/Image type, how?

Permalink
Hi All, I'm trying to set an attribute to a custom page type via a script but it seems like I'm missing something, to set the attribute of type image/file what do you need? the file name or the file id?

$attributes->setAttribute('product_image', 2);
$attributes->setAttribute('product_image', 'name.jpg');


I already have the file in the file manager, I just need to update the file for a particular page.

Any help is greatly appreciated, thanks.

mdzoidberg
 
12345j replied on at Permalink Reply
12345j
I'd think the file id, but Im not sure
mdzoidberg replied on at Permalink Reply
mdzoidberg
Thanks, I tried that but no go, got the following error

"Fatal error: Call to a member function getFileID() on a non-object"

Any other ideas?
hereNT replied on at Permalink Best Answer Reply
hereNT
It was giving you that error because it was expecting a file object in the save function - you need to first create a file object and then pass that in as your argument.

$fObj = File::getByID($fID);
$attributes->setAttribute('product_image', $fObj);
mdzoidberg replied on at Permalink Reply
mdzoidberg
Thanks hereNT, that did the trick!
loeppel replied on at Permalink Reply
Seems a bit weired to me that
$p->setAttribute('image', File::getByID($fid));

did not work for me, but your solution does. I think they should be semantically equal.
tsquared02 replied on at Permalink Reply
does anyone know how to get the id without the ID? let's say if I only know the image file name. how do I get the ID set the image attribute? I tried using File::getByHandle($imageName); but it didnt work.
mdzoidberg replied on at Permalink Reply
mdzoidberg
Anyone? please :)