Forums / Developer / Fetching related objects of a specific class

Fetching related objects of a specific class

Author Message

Brian Gerspacher

Friday 11 November 2005 11:43:32 am

I have user groups related to a product and want to retieve only the user groups related to that product (instead of all of the related content objects.) So far I can retieve all of the user groups in the system...

$userGroupClass =& eZContentClass::fetchByIdentifier('user_group');
$conditions = array('contentclass_id' => $userGroupClass->attribute('id'));
$groupObjects =& eZContentObject::fetchList(true, $conditions);

...and I know how to get all of the objects related to the product...

$productObject =& eZContentObject->fetch($myProductId);
$relatedObjects =& $productObject->relatedContentObjectArray();

...but I don't know how to get only the user groups related to the product. Is there an intuitive way to do this, or will I have to take one of the above approaches and loop over the results to pick out the objects I want?

Clemens T

Friday 11 November 2005 4:57:07 pm

I'd say just loop the related-objects array and create a new array and fill it with the object you want:

(pseudo code)

$myFilteredRelatedObject=array();
foreach($relatedObjects as $related)
{
      if($related->attribute('class_identifier')=='my_class_id')
      {
             $myFilteredRelatedObject[]=$related;
      }
}//$myFilteredRelatedObject contains the filtered objects.

off course this is pseudo code, but something like this should work (and yes.. I know that your approch is 'cleaner'.. but don't have any ez code here.. so can't check the ez classes.. that's why my answer is probably incomplete).

Good luck!
Clemens