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.

Data Model Questions

getting dataobject while looping through another dataobject


Reply

6 Posts   1196 Views

Avatar
Futureweb

4 July 2011 at 9:55pm Community Member, 233 Posts

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

4 July 2011 at 10:27pm Forum Moderator, 1796 Posts

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
Futureweb

4 July 2011 at 11:06pm Community Member, 233 Posts

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

4 July 2011 at 11:11pm (Last edited: 4 July 2011 11:12pm), Forum Moderator, 1796 Posts

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
Futureweb

4 July 2011 at 11:17pm Community Member, 233 Posts

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

4 July 2011 at 11:46pm Forum Moderator, 1796 Posts

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