Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Basic Data Model Question

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

Page: 1
Go to End
Author Topic: 453 Views
  • SamTheJarvis
    Avatar
    Community Member
    24 Posts

    Basic Data Model Question Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Basic Data Model Question Link to this post

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

  • SamTheJarvis
    Avatar
    Community Member
    24 Posts

    Re: Basic Data Model Question Link to this post

    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?

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Basic Data Model Question Link to this post

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

  • SamTheJarvis
    Avatar
    Community Member
    24 Posts

    Re: Basic Data Model Question Link to this post

    A WHOLE LOAD OF THINGS JUST MADE SENSE.

    Thank you martimiz!

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