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.

Data Model Questions /

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

getting dataobject while looping through another dataobject


Go to End


6 Posts   2027 Views

Avatar
JonShutt

Community Member, 244 Posts

4 July 2011 at 9:55pm

Hello,

I can't seem to get the logic of this one

I have a table of galleries, and a table of photos. I'm looping through the galleries table listing all these. But for each gallery, I want to check if there are any photos in the photos table attached to this gallery.

Eg

<% control getGalleries %>

<div><a href=''><strong>$title</strong></a>

<% control getImages %>
show an image + number of images in this gallery
<% end_control %>
</div>
<% end_control %>

Now, how to I tell the function getImages what the current ID from the getGalleries function is?

Avatar
swaiba

Forum Moderator, 1899 Posts

4 July 2011 at 10:27pm

Hi,

You could do something like this http://www.silverstripe.org/template-questions/show/13619#post304007#post304007 (I must mentioned that ajshort, an ss employee, has said this isn't the best way - but I have yet to have an update with his suggestion).

Alternatively if you can post more code then simple relationship getters should work as controls and it is more than likely you will not need to preload the data...

Avatar
JonShutt

Community Member, 244 Posts

4 July 2011 at 11:06pm

Hi - works great - thanks!

apart from one issue - it's returning an empty value, rather than nothing if if the subset returns no content.
i'm sure it's an if else thing, i just have to find it...

would be intersted to here what the 'idea' why of doing this is...

Avatar
swaiba

Forum Moderator, 1899 Posts

4 July 2011 at 11:11pm

Edited: 04/07/2011 11:12pm

for the "empty" items you can wrap your "control" within an "if" to remove...

<% if MyControl %>
<% control MyControl %>
do stuff
<% end_control %>
<% end_if %>

...that is - if this is your issuing - I'm just guessing.

And the reasons - I am avoiding need to do a lookup that might be dependant on a variable - because you cannot pass a variable as an argument to a function due to the parsing engine of the template making one pass only.

Avatar
JonShutt

Community Member, 244 Posts

4 July 2011 at 11:17pm

Hi,
thanks for the help swaiba, got it working by putting the line <% if Photo %> in the template, which is one of the values being returned. works fine.

<% control TheGallerySubDataObject %>
<% if Photo %>
<div class='galleryImage '>
<p>Description: $Description</p>
$Photo.SetWidth(100)
</div><% end_if %>
<% end_control %>

for some reason doing

<% if TheGallerySubDataObject %>
<% control TheGallerySubDataObject %>

didn't work - it stopped any info being writen to the page...

then again, it is late... might make more sense in the morning!

Avatar
swaiba

Forum Moderator, 1899 Posts

4 July 2011 at 11:46pm

Could well be because it's a DataObject and not a DataObjectSet...

if you do DataObject::get you'll get wither null or a DataObjectSet of values, with DataObject::get_one you'll get false or a DataObject. It is likely this is what is causing an issue... as you can see here...

		Debug::show(DataObject::get_one('TheGallerySubDataObject ','ID=99999999'));
		Debug::show(DataObject::get('TheGallerySubDataObject ','ID=99999999'));