Jump to:

23477 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » nested dataobject in template

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 903 Views
  • tv
    Avatar
    Community Member
    43 Posts

    nested dataobject in template Link to this post

    I have a CatalogItem DataObject that has_many VinylSlots and VinylSlot DataObject has_many Thumbnails.

    class CatalogItem extends DataObject {
    static $has_many = array(
    'VinylSlots' => 'VinylSlot'
    )
    }

    class VinylSlot extends DataObject {
    static $has_many = array(
    'Thumbnails' => 'VinylSlotPhoto'
    )
    }

    I have a Page on which I would like to loop through a filtered set of CatalogItems and get the VinylSlots' Thumbnails.

    Here is what I have that is not working:

    class SCBPage_Controller extends Page_Controller {
    public function getReleases() {
    $releases = DataObject::get('CatalogItem', 'ShoolChoirRelease = 1');
    $covers = new DataObjectSet();
    foreach($releases as $release) {
    $covers->push(new ArrayData(
    array(
    'Vinyl' => new DataObjectSet($release->VinylSlots())
    )
    ));
    }
    return $covers;
    }
    }

    And in the template:

    <% control Releases %>
    <% control Vinyl %>
    $Thumbnails
    <% end_control %>
    <% end_control %>

    Any thoughts on how I can pull the urls of the VinylSlots Thumbnails using a function in the SCB Controller?

    Thanks.

  • tv
    Avatar
    Community Member
    43 Posts

    Re: nested dataobject in template Link to this post

    this is how I was able to get this to work. I used two DataObject::get call, so I imagine this may not be the most eloquent way to handle this. If anyone has any suggestions on how to refactor this, I'm all ears!

    public function getReleases() {
    $releases = DataObject::get('CatalogItem', 'ShoolChoirRelease = 1');
    $doSet = new DataObjectSet();

    foreach($releases as $release) {
    $id = $release->ID;
    $slot = DataObject::get('VinylSlot', 'CatalogItemID =' . $id );
    $thumb = $slot->First()->VinylSlotThumb();
    $fullsize = $slot->First()->VinylSlotFull();

    $doSet->push(new ArrayData(
    array(
    'Band' => $release->ArtistName,
    'CatalogNum' => $release->CatalogNumber,
    'CatalogTitle' => $release->CatalogTitle,
    'Format' => $release->Format,
    'Thumb' => $thumb,
    'FullImg' => $fullsize
    )
    ));
    }

    return $doSet;
    }

    note that VinylSlotThumb() and VinylSlotFull() are functions on the VinylSlot class that grab associated Imgs:

    public function VinylSlotThumb() {
    $covers = $this->Thumbnails(null,null,null,1);
    return $covers->First();
    }

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: nested dataobject in template Link to this post

    One thing I notice you got wrong in your first example was this:

    array(
    'Vinyl' => new DataObjectSet($release->VinylSlots())
    )

    It should be this:

    array(
    'Vinyl' =>$release->VinylSlots()
    )

    Alternatively, you can just return $releases and then put <% control VinylSlots %> into your template. This would be the way that I would most likely do it:

    class SCBPage_Controller extends Page_Controller {
    public function Releases() {
    return DataObject::get('CatalogItem', 'ShoolChoirRelease = 1');
    }
    }

    And in the template:

    <% control Releases %>
    <% control VinylSlots %>
    $Thumbnails
    <% end_control %>
    <% end_control %>

    In SilverStripe 3, you could follow the relation and therefore get the data without nested DataObject::get calls:

    class SCBPage_Controller extends Page_Controller {
    public function Vinyl() {
    return CatalogItem::get()->filter('ShoolChoirRelease', 1)->relation('VinylSlots');
    }
    }

    Your template would then become:

    <% loop Vinyl %>
    $Thumbnails
    <% end_loop %>

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