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.

Customising the CMS /

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

Basic Data Model Question


Go to End


5 Posts   974 Views

Avatar
SamTheJarvis

Community Member, 24 Posts

3 June 2011 at 7:28am

Hi Guys,

Could someone perhaps give me a hand here,

I have a gallery of photos, each photo has a category, and a category has a category holder.

In the category holder, a selected image is used to represent each category, and is displayed on the category holder. I have this working fine like so:

	public function CategoryPhotos() {
		$categories = DataObject::get("PhotoCategory");
		$primaryphotos = new DataObjectSet();
		foreach($categories as $category){
			$photo = DataObject::get_one("Photo", "ID=".$category->PrimaryPhotoID);
			$primaryphotos->push($photo);
		}
		return $primaryphotos;
	}

Basically, I want to also include the link and title of that category in the dataobjectset, so I can use it in the template, so the user can click through to the photo category.

Some help here would be grandly appreciated!

Thanks

Avatar
martimiz

Forum Moderator, 1391 Posts

11 June 2011 at 11:36pm

A simpler way to do this is move the function to retrieve the first photo from the CategoryHolder to the CategoryPage class itself (note: not the page controller!). Something like this:

function FirstImage() {
	return DataObject::get_one('Photo', "ID = $this->ID");
}

Then in your CategoryHolder template you can do something like this (assuming Photo has_many Images called 'Image'):

<% control Categories %>
	<a href="$Link">
	<% control FirstImage %>
		$Image.setWidth(100)
	<% end_Control %>
	</a>
<% end_control %>

Avatar
SamTheJarvis

Community Member, 24 Posts

12 June 2011 at 3:59am

I tried that before! Still, it's not working, the title and link is being displayed, but the photo isn't.

<% control Categories %>
	<a href="$Link">
		$Title
		<% control PrimaryPhoto %>
		$ImageFile
		<% end_control %>
		</a>
<% end_control %>

PhotoCategory has many dataobjects called Photo, and photo has the fields: ImageFile and Caption.

In my PhotoCategory_Controller:

	public function PrimaryPhoto() {
		return DataObject::get_one("Photo", "ID=".$this->PrimaryPhotoID);
	}

This returns the selected (primary) photo on the PhotoCategory page, but not in any nested controls.

Any ideas?

Avatar
martimiz

Forum Moderator, 1391 Posts

12 June 2011 at 5:38am

As I mentioned: don't put the function in your PhotoCategory_Controller, but in the PhotoCategory (page) class itself :-)

Althoug the template is handled by your CategoryHolder_Controller, the control structure <% control Categories %> will only return a bunch of PhotoCategory Pages (= DataObjects) and NOT their controllers, so every method in the PhotoCategory_Controller is out of reach within this template...

Avatar
SamTheJarvis

Community Member, 24 Posts

12 June 2011 at 6:04am

A WHOLE LOAD OF THINGS JUST MADE SENSE.

Thank you martimiz!