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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Accessing Nested ImageDataObjectManager


Reply

7 Posts   1068 Views

Avatar
wainui

1 June 2010 at 5:15pm (Last edited: 1 June 2010 5:16pm), Community Member, 56 Posts

Hi,

I have been using the sample here.. http://silverstripe.org/dataobjectmanager-module-forum/show/274900#post274900..

Basically this is to show products and I have replaced the standard ImageField with a manager

// replaced this
new ImageField('ProductImage')


// with a ImageDataObjectManager like
   new ImageDataObjectManager(
      $this,
      'Galleries',
      'Gallery',
      'Attachment',
      array('Description' => 'Description')
      )
   );

This is all great... I am wanting to take it a bit further though and show the first image from the gallery in the backend... kindof like showing a thumbnail from the ProductImage. ie: http://www.ssbits.com/adding-a-thumbnail-to-a-dataobjectmanager-or-complex-table-field/

Any Ideas.. I tried but it was asking me for a "fortemplate" function.. I added this to the gallery dataobject but dont know how to access data inside that.. any ideas.. my gallery object is included below..

<?php
class Gallery extends DataObject
{
   static $db = array (
      'Name' => 'Text',
      'Description' => 'Text'   );

   static $has_one = array (
      'Attachment' => 'Image',
      'Product' => 'Product'
   );

   public function getCMSFields_forPopup()
   {
      return new FieldSet(
         new TextField('Name'),
         new TextareaField('Description'),
         new FileIFrameField('Attachment')
      );
   }

   function forTemplate() {
   // how to access gallery objects here..
   // namely get first image from gallery items.
   }
   
}

Avatar
UncleCheese

2 June 2010 at 1:36am 4085 Posts

I'm lost. Why do you need a forTemplate() method?

Avatar
wainui

2 June 2010 at 8:59am (Last edited: 2 June 2010 9:00am), Community Member, 56 Posts

HI Uncle Cheese..
For some reason I cant get this sussed.. I want tor return the first image from the gallery (imageDataObject)..
however it seems to return an object which it sends to the forTemplate function.

I am using a getThumbnail script.. it was returning an object which made it try and call the forTemplate function.. lost here :(

I have got his far..

// in my StorePage
public function getCMSFields()
{
$fields = parent::getCMSFields();
$manager = new DataObjectManager(
      $this,
      'Products',
      'Product',
      array(
      'Thumbnail' => 'Image',
      'getGalleryThumbnail' => 'Gallery', // :)
      'Title' => 'Title',
      'Description' => 'Description',
      'Price' => 'Price',
      'OutOfStock' => 'Out Of Stock'
),
'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
         ,''// Filter clause
         ,''// Sort clause
         // Join clause
);

$fields->addFieldToTab("Root.Content.Products", $manager);

return $fields;
}
}


Then in my product dataobject

function getGalleryThumbnail() {
    $myGallery = DataObject::get_one( "Gallery", "ProductId = {$this->ID}");
    // but how to actually get thhe image and return it here :(
   
    return ($myGallery->AttachmentID); // does not work...
}

Avatar
UncleCheese

2 June 2010 at 9:21am 4085 Posts

Why not $gallery->Attachment()?

Avatar
wainui

2 June 2010 at 10:23am Community Member, 56 Posts

OK.. thanks for the heads up..

managed tio get it working using the code below..
one of the things I missed was checking if the $Gallery was valid.. as calling $Gallery->Attachment(); on something which didnt exist was breaking the script.

Thanks for your help.. now to figure out how to get the first image or does get_one return the first sorted image? will check.

thanks again.

function getGalleryThumbnail()
   {
   $Gallery = DataObject::get_one("Gallery", "ProductId = {$this->ID}");
   if(!$Gallery)
   {
      return "no image";
   }
   $myImage = $Gallery->Attachment();
   if($myImage)
   {
       return $myImage->CroppedImage(150,50);
      } else{
       return "no image";
    }
   
   }

Avatar
UncleCheese

2 June 2010 at 11:54am (Last edited: 2 June 2010 11:56am), 4085 Posts

Yeah, it will get the default sort, which in the case of ImageGalleryItem being decorated with SortableDataObject, will be the SortOrder field. You can add arguments to the function for filter and sort. $obj->Attachment($filter, $sort);

That function is pretty bloated... try cleaning it up a bit..

function getGalleryThumbnail()
{
if($gallery = DataObject::get_one("Gallery", "ProductId = {$this->ID}"))
return ($img = $gallery->Attachment()) ? $img->CroppedImage(150,50) : "no image";
return "no gallery";
}

Avatar
wainui

2 June 2010 at 2:12pm Community Member, 56 Posts

mmm.. that looks better :)

thanks for your help.