Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » getting dataobject while looping through another dataobject

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

Page: 1
Go to End
Author Topic: 1153 Views
  • Futureweb
    Avatar
    Community Member
    232 Posts

    getting dataobject while looping through another dataobject Link to this post

    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?

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: getting dataobject while looping through another dataobject Link to this post

    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...

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: getting dataobject while looping through another dataobject Link to this post

    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...

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: getting dataobject while looping through another dataobject Link to this post

    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.

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: getting dataobject while looping through another dataobject Link to this post

    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!

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: getting dataobject while looping through another dataobject Link to this post

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

    1153 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.