Jump to:

3462 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Compiling a list of unique data from a group of DataObjects

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

Page: 1
Go to End
Author Topic: 1043 Views
  • flipsidenz
    Avatar
    Community Member
    49 Posts

    Compiling a list of unique data from a group of DataObjects Link to this post

    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?

  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

    Re: Compiling a list of unique data from a group of DataObjects Link to this post

    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( );

  • flipsidenz
    Avatar
    Community Member
    49 Posts

    Re: Compiling a list of unique data from a group of DataObjects Link to this post

    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

  • flipsidenz
    Avatar
    Community Member
    49 Posts

    Re: Compiling a list of unique data from a group of DataObjects Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

    Re: Compiling a list of unique data from a group of DataObjects Link to this post

    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;
       }

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