Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Accessing Nested ImageDataObjectManager

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 993 Views
  • wainui
    Avatar
    Community Member
    56 Posts

    Accessing Nested ImageDataObjectManager Link to this post

    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.
       }
       
    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: Accessing Nested ImageDataObjectManager Link to this post

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

  • wainui
    Avatar
    Community Member
    56 Posts

    Re: Accessing Nested ImageDataObjectManager Link to this post

    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...
    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: Accessing Nested ImageDataObjectManager Link to this post

    Why not $gallery->Attachment()?

  • wainui
    Avatar
    Community Member
    56 Posts

    Re: Accessing Nested ImageDataObjectManager Link to this post

    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";
        }
       
       }

  • UncleCheese
    Avatar
    4085 Posts

    Re: Accessing Nested ImageDataObjectManager Link to this post

    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";
    }

  • wainui
    Avatar
    Community Member
    56 Posts

    Re: Accessing Nested ImageDataObjectManager Link to this post

    mmm.. that looks better

    thanks for your help.

    993 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.