7913 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Accessing Nested ImageDataObjectManager
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 897 Views |
-
Accessing Nested ImageDataObjectManager

1 June 2010 at 5:15pm Last edited: 1 June 2010 5:16pm
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.
}
} -
Re: Accessing Nested ImageDataObjectManager

2 June 2010 at 1:36am
I'm lost. Why do you need a forTemplate() method?
-
Re: Accessing Nested ImageDataObjectManager

2 June 2010 at 8:59am Last edited: 2 June 2010 9:00am
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 dataobjectfunction 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...
} -
Re: Accessing Nested ImageDataObjectManager

2 June 2010 at 10:23am
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";
}
} -
Re: Accessing Nested ImageDataObjectManager

2 June 2010 at 11:54am Last edited: 2 June 2010 11:56am
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";
} -
Re: Accessing Nested ImageDataObjectManager

2 June 2010 at 2:12pm
mmm.. that looks better
thanks for your help.
| 897 Views | ||
|
Page:
1
|
Go to Top |

