Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Thumbnail for many_many relationship in the list of fields on the back end

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: 637 Views
  • Johnny
    Avatar
    Community Member
    34 Posts

    Thumbnail for many_many relationship in the list of fields on the back end Link to this post

    Hi UncleCheeze!

    There's something I've been trying to do, it doesn't sound too complicated, and it would be a really nice feature for my client :

    I'm programming a web site for an art gallery. The have «paintings» related to «exhibitions». This is a many-many relationship. Paintings have an image linked to them (has_one), as well as a Title, the creator's name, the year of creation, etc.

    In the CMS, when it's time to link paintings to exhibitions, it works well, but it's hard to find out which painting is which because there are many « untitled » paintings. It would be awesome to have a thumbnail that shows the image related to a painting in a column.

    Tell me if it's not clear!

    thanks,

    JP

  • Webdoc
    Avatar
    Community Member
    329 Posts

    Re: Thumbnail for many_many relationship in the list of fields on the back end Link to this post

    Can u show your php code i think i can help

  • Johnny
    Avatar
    Community Member
    34 Posts

    Re: Thumbnail for many_many relationship in the list of fields on the back end Link to this post

    I'm not sure the code will help... The feature I request could be used on any ManyMany relationship that have an image linked to the other side of the relation.

    But, in any cases, I'll send an excerpt of the code:

    class ExpoPage extends Page {
       public static $db = array(
       );

       public static $has_one = array(
       );

       public static $has_many = array(
       );
       
       static $many_many = array(
          'ExhibitingWorks' => 'ArtistWork'
       );

       function getCMSFields () {
          $fields = parent::getCMSFields();

          $x = new ManyManyDataObjectManager(
             $this,
             'ExhibitingWorks',
             'ArtistWork',
             array (
                'Title' => "Titre de l'oeuvre",
                "CONCAT(FirstName,' ', LastName)"=>'Artiste',
                "Year" => 'Année'
             ),
             null,
             null, 'Title', "LEFT JOIN ArtistPage ON (ArtistWork.CreatorID=ArtistPage.ID)"
          );
          $x->setPermissions (Array()); //WE DON'T WANT THEM TO EDIT PAINTINGS, WE WANT THEM TO USE THE CHECKBOX TO CHOOSE WHICH PAINTINGS ARE EXHIBITED AND WHICH AREN'T
          $x->setPageSize (8999);
          $x->setTitle ("Liste des oeuvres");
          $fields->addFieldToTab("Root.Content.ListeDesOeuvres", $x);

          return $fields;
       }
    }

    class ArtistWork extends DataObject {
       public static $db = array (
          'Title' => 'Text',
          'Medium' => 'Text',
          'Year' => 'Int',
          'Height' => 'Float', /* All dimensions are in CM, converted to inches when english */
          'Width' => 'Float', /* All dimensions are in CM, converted to inches when english */
          'Depth' => 'Float' /* All dimensions are in CM, converted to inches when english */
       );

       public static $has_one = array(
          'Creator' => 'ArtistPage', //from a has_many relationship on ArtistPage
          'Image' => 'Image'
       );
       
       static $belongs_many_many = array (
          'ExpoPages' => 'ExpoPage'
       );
       
       function getCMSFields_forPopup () {
          $fields = new FieldSet();
          
          $fields->push (new TextField ('Title', 'Titre'));
          $fields->push (new TextField ('Medium', 'Médium'));
          $fields->push (new TextField ('Year', 'Année (ex: 2010)'));
          $fields->push (new TextField ('Height', 'Hauteur (en CM sans le CM, ex: 230)'));
          $fields->push (new TextField ('Width', 'Largeur'));
          $fields->push (new TextField ('Depth', 'Profondeur'));

          $uploader = new ImageUploadField ('Image', "Image de l'oeuvre");
          $uploader->uploadFolder = "oeuvres";
          $fields->push ($uploader);
          
          return $fields;
       }
    }

  • Webdoc
    Avatar
    Community Member
    329 Posts

    Re: Thumbnail for many_many relationship in the list of fields on the back end Link to this post

    try this
    $x = new ManyManyDataObjectManager(
    $this,
    'ExhibitingWorks',
    'ArtistWork',
    array (
    'Title' => "Titre de l'oeuvre",
    "CONCAT(FirstName,' ', LastName)"=>'Artiste',
    "Year" => 'Année' ,
    "Image" => 'Image translation'
    ),

  • Johnny
    Avatar
    Community Member
    34 Posts

    Re: Thumbnail for many_many relationship in the list of fields on the back end Link to this post

    Hmmm...

    I tried it and it gives the SQL Error : « Unknown column 'Image' in 'field list' »

    I tried with ImageID instead of Image, and it shows the ID of each images, in the column titled « Image translation », like this code's behavior has been expected!

    Thanks anyway!

  • UncleCheese
    Avatar
    4085 Posts

    Re: Thumbnail for many_many relationship in the list of fields on the back end Link to this post

    Just use a custom getter..

    public function getDOMThumbnail() {
    if($i = $this->YourImageField()) {
    return $i->CroppedImage(50,50);
    }
    }

    And in your headings array, you just need

    'DOMThumbnail' => 'Thumbnail'

    ---------------
    Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

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