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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

has-many images using imagedataobjectmanager


Go to End


3 Posts   2227 Views

Avatar
web2works

Community Member, 50 Posts

16 September 2010 at 11:43pm

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 {

}

Avatar
Martijn

Community Member, 271 Posts

17 September 2010 at 12:10am

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'
  ); 

}

Avatar
web2works

Community Member, 50 Posts

17 September 2010 at 2:40am

Edited: 17/09/2010 2:42am

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'
);
}