Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] Finding Image Relationships

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

Page: 1
Go to End
Author Topic: 418 Views
  • Bosie
    Avatar
    Community Member
    2 Posts

    [SOLVED] Finding Image Relationships Link to this post

    Hello all,

    I'm new here! I wonder if you can help me solve something.

    I'm trying to build a module with some generic capabilities. Part of that is trying to find Images that are related to pages, of many page types, types that I can't predict - this is a generic module, as I said. With relationships to classes that extend Image, again, that I can't predict.

    So, I know that images can be added to pages either through the normal way in the CMS, which gives me no problem, I can look that up. But otherwise, images often through a has_one/many/many_many relationship with an arbitrary name for the relationship, using uploadify, MultipleFileUploadField or whatever. And these relationships are given arbitrary names by whoever developed the site.

    For example, one site has a page with a type PortfolioPage. That has a has_many relationship to PortfolioImage which entends Image.

    // in PortfolioPage.php
       static $has_many = array(
          'PortfolioEntryImages' => 'PortfolioImage'
       );

    // in PortfolioEntryImage.php
    class PortfolioEntryImage extends Image {
       static $has_one = array (
          'PortfolioPage' => 'PortfolioPage'
       );

       //some specific stuff here

    }

    In another site, the same thing:

    // in RoomPage.php
       static $many_many = array(
          'RoomImages' => 'RoomImage'
       );

    // in RoomImage.php
    class RoomImage extends Image {
       static $belongs_many_many = array (
          'RoomPage' => 'RoomPage'
       );

       //some specific stuff here
    }

    My class will have an instance of one of these page types (not just those, it could be *any* class that has extended Page, and like above, it could have relationships to classes that extend Image).

    What I need to do is find those images, without knowing the name of the relationship. And then store the name of the relationship for future use (to grab the images when I need to).

    So, I'm thinking that maybe I can run through the class that extends Page (PortfolioPage for example) and look at all the properties it has, find out the types of objects they are, to see if they extend image. That feels pretty horrible. And I'm not sure how to do that ;)

    But I'm guessing/hoping there's a nicer way?

    Sorry, this is probably waaay too long winded, but I hope it gets my problem across

    Thank you!

    Bosie

  • Bosie
    Avatar
    Community Member
    2 Posts

    Re: [SOLVED] Finding Image Relationships Link to this post

    Simon on IRC put me on the right track (thanks!):

    foreach($linkedPage->has_many() as $name=>$class) {
    //then test for superclass here
    }

    Awesome.

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