Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Many images

Our old forums are still available as a read-only archive.

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

Page: 1 2 3
Go to End
Author Topic: 9266 Views
  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Many images Link to this post

    Hi,

    I am also setting up a gallery, and have copied your code.

    The ImageAttachement tab is showing up. However, in my popup for adding an image, I get the following error:

    "FATAL ERROR: ComplexTableField::DetailForm() Cannot automatically determine 'has-one'-relationship to parent, please use setParentClass() to set it manually
    At line 306 in C:\wwwroot\wamp\www\silverstripe-v2.2.2\sapphire\forms\ComplexTableField.php"

    What am I doing wrong?

    Thanks,
    Garrett

  • UncleCheese
    Avatar
    4085 Posts

    Re: Many images Link to this post

    This is usually an easy fix. Make sure that every has_many relationship you have corresponds to an object that has a has_one relationship with the other object, and do a /db/build.

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Many images Link to this post

    Hi,

    Thanks very for your reply..... However, I did copy this code exactly, so I still am not sure what I am doing wrong. In my ImageAttachment (extends DataObject) class, I have:

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

    And in my ProjectPage (extends Page) class, the pagetype which will contain the unlimited images, I have:

    static $has_many = array(
    'ImageAttachments' => 'ImageAttachment'
    );

    As I said, the ImageAttachment tab IS showing up, but in the POPUP for adding an image attachment, I am getting the "Cannot automatically determine 'has-one'-relationship to parent, please use setParentClass() to set it manually" error.

    Your continued help would be greatly appreciated!

    Thanks again,
    Garrett

  • UncleCheese
    Avatar
    4085 Posts

    Re: Many images Link to this post

    Again, for every has_many, you need a reciprocating has_one. Your ImageAttachment class needs a has_one with ProjectPage.

    And I'm not so sure about extending the DataObject class for your images. Probably better to extend image, and use the generateXXX() functions.

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Many images Link to this post

    Hi-- and thanks again for your help. When you say, "Your ImageAttachment class needs a has_one with ProjectPage," I am not sure how to translate that into code, as I haven't seen any examples of this exact architecture.

    Here is the $has_one in my ImageAttachment class:

    static $has_one = array(
       'ImageMain' => 'Image',
       'ImageThumbnail' => 'Image'
    );

    How do I refer to the ProjectPage? What do I add here?

    'ProjectPage' => '?????'

    //Garrett

  • UncleCheese
    Avatar
    4085 Posts

    Re: Many images Link to this post

    static $has_one = array(
    'ImageMain' => 'Image',
    'ImageThumbnail' => 'Image'
    'ProjectPage' => 'ProjectPage'
    );

    /db/build?flush=1

    Granted, you could really give it any name you want, since you're probably never going to use it as a property. But this will help the DB builder to see the relationship and you won't have to worry about an ambiguous parent/child relationship on your CTF.

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Many images Link to this post

    Ok, that made the error go away-- thanks! Now ONE more problem: the Images do not appear to be saving to the database. In other words, in the popup, I upload the image, click save, and then when I close the popup nothing has been added to the list.

    Here is my code once again:

    ProjectPage
    ------------

    class ProjectPage extends Page {

       static $db = array(
           'ProjectChallenge' => 'Text'
          ,'ProjectSolution' => 'Text'
          ,'ProjectResults' => 'Text'
          ,'ImageMainTitle' => 'Text'
       );

       static $has_one = array(
           'ImageMain' => 'Image',
           'ThumbnailMain' => 'Image'
       );

       static $has_many = array(
          'ImageAttachments' => 'ImageAttachment'
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();
          
       $fields->addFieldToTab("Root.Content.Main", new TextAreaField('ProjectChallenge', 'The Challenge'), 'Content');
       $fields->addFieldToTab("Root.Content.Main", new TextAreaField('ProjectSolution', 'The Solution'), 'Content');
           $fields->addFieldToTab("Root.Content.Main", new TextAreaField('ProjectResults', 'The Results'), 'Content');
          $fields->addFieldToTab('Root.Content.Main', new ImageField('ImageMain', 'Main Image'), 'Content');
          $fields->addFieldToTab('Root.Content.Main', new ImageField('ThumbnailMain', 'Main Thumbnail'), 'Content');
          $fields->addFieldToTab('Root.Content.Main', new TextField('ImageMainTitle', 'Main Image Title'), 'Content');
          
          $fields->removeFieldFromTab("Root.Content.Main","Content");
          
          $imagetable = new ComplexTableField(
             $this,
             'ImageAttachments', // relation name
             'ImageAttachment', // object class
             ImageAttachment::$field_names, // fields to show in table
             ImageAttachment::getCMSFields_forPopup(), // form that pops up for edit
             "ImageID = {$this->ID}", // a filter to only display item associated with this page
             "Name ASC" // Sort by name
          );
          $fields->addFieldToTab('Root.Content.ImageAttachments', $imagetable);
       
          return $fields;
       }
    }

    ------------------
    ImageAttachment
    ------------------
    <?php

    class ImageAttachment extends DataObject {
       static $db = array(
          'Title' => 'Text'
       );
       
       static $has_one = array(
          'Image' => 'Image',
          'ProjectPage' => 'ProjectPage'
       );
       
       static $field_names = array(
          'Image' => 'Image',
          'Title' => 'Title'
       );
       
       function getCMSFields_forPopup() {
          
          $fields = new FieldSet();
          
          $fields->push(new TextField('Title', 'Title'));
          $fields->push(new ImageField('Image', 'Image'));
       
          return $fields;
       }
    }

    ?>

  • UncleCheese
    Avatar
    4085 Posts

    Re: Many images Link to this post

    This seems a little suspect..

    "ImageID = {$this->ID}", // a filter to only display item associated with this page

    I think you want ProjectPageID = {$this->ID}

    9266 Views
Page: 1 2 3
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.