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 /

[SOLVED] Finding Image Relationships


Reply


2 Posts   520 Views

Avatar
Bosie

Community Member, 2 Posts

18 July 2012 at 5:35pm

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

Avatar
Bosie

Community Member, 2 Posts

18 July 2012 at 6:34pm

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

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

Awesome.