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 /

Compiling a list of unique data from a group of DataObjects


Reply


5 Posts   1127 Views

Avatar
flipsidenz

Community Member, 49 Posts

21 June 2011 at 11:40pm

Apologies if this question has an overly obvious answer. I am just getting my head around SilverStripe. Loving it so far.

Using the E-Commerce module, I am dealing with the ProductGroups class. Each ProductGroup pulls a number of products.

The Products objects have a "has_one" relationship with a DataObject "Brand" which I created.

On each ProductGroup, I am wanting to pull a unique list of brands available within it's selection of products.

Aside from doing an Sqlquery with hand written mysql, can anyone offer some insight into how I might pull this distinct list of brands at the ProductGroup level?

Avatar
swaiba

Forum Moderator, 1808 Posts

22 June 2011 at 12:43am

I'm not familiar with the eCommerce, but if you have a has_many / many_many relationship like this...

$has_many = array(
'ManyManyRelationshipName' => 'RelObject'
);

you will get a ComponentSet when using the relationship getter and that has a method to return an array of ids...

$cos = $obj->ManyManyRelationshipName();
$arrIDS = $cos->getIdList( );

Avatar
flipsidenz

Community Member, 49 Posts

22 June 2011 at 9:15am

Edited: 22/06/2011 9:16am

Thanks for reply to my query.

I'm a bit lost though. Here's my relationship information for ProductGroup, Product and Brand.

ProductGroup:

public static $has_one = array();
   
   public static $has_many = array();
   
   public static $many_many = array();
   
   public static $belongs_many_many = array(
      'Products' => 'Product'
   );

Product:

public static $has_one = array(
      'Image' => 'Product_Image',
      'Brands' => 'Brand'
   );
   

   
   public static $many_many = array(
      'ProductGroups' => 'ProductGroup'
   );

Brand

static $has_many = array (
'Products' => 'Product'
);

I tried using your suggested code inside a function in ProductGroup, but it's returning an empty array.

public function Brands(){
   
      $cos = $this->Products();
      $arrIDS = $cos->getIdList( );
      
      return print_r($arrIDS);
   }

With that in mind, even if it was working, it would be supplying a list of product ID's, correct? Whereas mt aim is to pull a unique list of brands.

Apologies for my lack of understand. Hope someone has time to help :)

Avatar
flipsidenz

Community Member, 49 Posts

22 June 2011 at 9:28am

Looking at this thread post:

http://www.silverstripe.org/data-model-questions/show/13120#post287458

If I were to replace RequiredRoutewayCourse with 'Brand', I'm trying to achieve the same results.

I just don't know what the poster means by:

...and just look at the RequiredRoutewayCourse DataObject for your answer, of course, that will affect the Admin use-case.

I have my Product-Brand relationship set up as 1-many, but I am unsure how ProductGroup would gather that information :S

Avatar
swaiba

Forum Moderator, 1808 Posts

22 June 2011 at 10:25am

well I missed the mark there by a margin...

whenever I think, "hmmm how would i do that with silverstripes ORM?" for too long I usually write the SQL and call this function...

   function GetDataObjectSetFromDBQuery($strSQL) {
      $records = DB::query($strSQL);
      $dos = new DataObjectSet();
      $rec = $records->next();
      while ($rec) {
         $do = new DataObject($rec);
         $dos->push($do);
         $rec = $records->next();
      }
      return $dos;
   }