Jump to:

7938 Posts in 1541 Topics by 945 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » has-many images using 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: 1766 Views
  • web2works
    Avatar
    Community Member
    50 Posts

    has-many images using imagedataobjectmanager Link to this post

    Hi, I trying to create a portfolio of website. Each website with an unlimited amount of images. I have got it working with the new ImageField but this imits the amount of images I can upload.

    At the moment I have it working to display all the images but there is no relation to the website just all the images I have uploaded.

    Website.php
    <?php
    class Website extends Page {
       
       static $db = array (
          'Featured' => 'Boolean',
          'Completion' => 'Date'
       );
       
       static $has_one = array(
          'Website_Browse' => 'Website_Browse'
       );
       
       static $has_many = array(
    'Images' => 'Image'
    );
          
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->renameField('Title', 'Website Title');
          $fields->addFieldToTab('Root.Content.Main', new CheckboxField(
          $name = "Featured",
             $title = "<strong>Featured Website</strong> - Check to display on the homepage"),
             'Content');
          $fields->addFieldToTab("Root.Content.Main", new DateField('Completion'));

          $images = new ImageDataObjectManager(
             $this,
             'Images',
             'Image'
          );
          $fields->addFieldToTab("Root.Content.Images",$images);
          
    return $fields;
    }
    }

    class Website_Controller extends Page_Controller {
       
    }

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: has-many images using imagedataobjectmanager Link to this post

    You need a $has_one at the Image as well.

    I would extend Image and add the $has_one there:

    class MyImage extends Image{

    static $has_one = array(
    'Website'
    );

    }

    class Website extends Page {

    static $has_many = array(
    'Images' => 'MyImage'
    );

    }

  • web2works
    Avatar
    Community Member
    50 Posts

    Re: has-many images using imagedataobjectmanager Link to this post

    That worked briliantly thank you very much.

    Another little query I am stuck next with. On the homepage I want to display 5 of the latest Websites although the part I can't get to work is the previously relation for the portfolios

    Currently I have:
    <ul>
       <% control LatestWebsites %>
       <li>
          <a href="$Link" title="Project title here">
             <% control Portfolios %>
                $Image
             <% end_control %>
          </a>
          <h3><a href="$Link" title="Project title here">$Title</a></h3>
          <p>$Content.LimitWordCountXML(30) <a href="$Link" title="Project title here">More...</a></p>
       </li>
       <% end_control %>
    </ul>

    ----- Page.php
    // Latest Featured Websites
       function LatestWebsites($num=10) {
       return DataObject::get("Websites", "Featured = 1", "Completion DESC", "", $num);
    }

    ----- Homepage.php
    <?php
    class Homepage extends Page {
    static $has_many = array('Slogans' => 'Slogans');

    public function getCMSFields(){
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Slogans", new DataObjectManager(
    $this,
    'Slogans',
    'Slogans',
    array(
    'SloganText'=> 'Text',
    'CSSid' => 'CSS ID',
    'SloganImage' => 'Image',
    'SloganBackground' => 'Background'
    ),
    'getCMSFields_forPopup'
    ));
    return $fields;
    }
    }
    class Homepage_Controller extends Page_Controller {}

    ----- Websites.php
    class Websites extends Page {
       static $db = array (
          'Featured' => 'Boolean',
          'Completion' => 'Date'
       );
       static $has_many = array('Portfolios' => 'Portfolios');
       static $has_one = array('WebsitesBrowse' => 'WebsitesBrowse');
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->renameField('Title', 'Website Title');
          $fields->addFieldToTab('Root.Content.Main', new CheckboxField(
          $name = "Featured",
             $title = "<strong>Featured Website</strong> - Check to display on the homepage"),
             'Content');
          $fields->addFieldToTab("Root.Content.Main", new DateField('Completion'));

          $images = new ImageDataObjectManager(
             $this,
             'Portfolios',
             'Portfolios'
          );
          $fields->addFieldToTab("Root.Content.Portfolios",$images);
          
    return $fields;
    }
    }
    class Websites_Controller extends Page_Controller {}

    ----- Portfolio.php
    <?php
    class Portfolios extends Image {
       static $has_one = array(
          'Websites' => 'Websites'
       );
    }

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