Dave
Dave

Reputation: 515

SilverStripe Image DataObject not available after creation

I have this code that creates a new Image based on a new file added to the filesystem but not yet in the DB:

$image = Image::create();
$image->Filename = 'assets/Art/e3434cc7-d348-491a-9dc8-325af3d9086d.jpg';
$image->write();

$images = Image::get();
$image = $images->last();

$vd = new ViewableData();
$ad = new ArrayData(array(
   'Image' => $image
));
$strHTML = $vd->customise($ad)->renderWith('Art');

Art.ss contains only $Image.SetWidth(100)

Ignoring the fact the query doesn't look up by ID or whatever... why is the image only rendered into $strHTML if I retrieve the image from the DB after creating? If I delete the code below, $strHTML is empty:

$images = Image::get();
$image = $images->last();

Upvotes: 1

Views: 90

Answers (1)

Jono
Jono

Reputation: 166

There is a setter available so using that alone may help, but you also may need to assign the ID of the parent folder to the object:

$pFolder = Folder::find_or_make('Art');
if ($pFolder) {
    $image->setParentID($pFolder->ID);
    $image->setFilename('assets/Art/e3434cc7-d348-491a-9dc8-325af3d9086d.jpg');
}

Upvotes: 1

Related Questions