Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObject::get method

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 4330 Views
  • zenmonkey
    Avatar
    Community Member
    524 Posts

    DataObject::get method Link to this post

    I'm trying the build a product review system, But I'm having trouble writing a function to calculate the average Rating. I can't seem to to get at the Ratings fields using DataObject::get.

    Here is my Review Data Object

    class Review extends DataObject
    {
       static $db = array (
          'Date' => 'Date',
          'Name' => 'Text',
          'Email' => 'Text',
          'Title' => 'Text',
          'Location' => 'Text',
          'Rating' => 'Int',
          'Review' => 'HTMLText',
          'Approval' => 'Boolean'
       );
       
       static $has_one = array (
          'Product' => 'Product'
       );
       
       
       
       public function getCMSFields_forPopup()
       {
          $ratingArray = array (
                       '1' => '1',
                       '2' => '2',
                       '3' => '3',
                       '4' => '4',
                       '5' => '5'
                       );
          
          return new FieldSet(
             new CheckboxField('Approval'),
             new CalendarDateField('Date'),
             new TextField('Name'),
             new EmailField('Email'),
             new TextField('Title'),
             new TextField('Location'),
             new DropdownField('Rating','Rating',$ratingArray),
             new TextareaField('Review')
          );
       }
       

    }

    And this is the query in my Product Page:

    $reviewContent = array(DataObject::get("Review","Approval = 1 AND ProductID =" . $this->ID));

    When I Save the OUtput to an Array this is what I get:

    Array
    (
    [0] => DataObjectSet Object
    (
    [items:protected] => Array
    (
    [0] => Review Object
    (
    [destroyed] =>
    [record:protected] => Array
    (
    [ID] => 1
    [ClassName] => Review
    [Created] => 2009-07-30 10:04:03
    [LastEdited] => 2009-07-30 10:04:03
    [Date] => 2009-07-30
    [Name] => Stewie
    [email= => stewie@loverschoice.tv] => [email=stewie@loverschoice.tv]stewie@loverschoice.tv
    [Title] => Wow
    [Location] => Toronto, Canada
    [Rating] => 5
    [Review] => This is the Greatest product I have ever used!
    [Approval] => 1
    [ProductID] => 3
    [RecordClassName] => Review
    )

    [changed:protected] => Array
    (
    )

    [original:protected] => Array
    (
    [ID] => 1
    [ClassName] => Review
    [Created] => 2009-07-30 10:04:03
    [LastEdited] => 2009-07-30 10:04:03
    [Date] => 2009-07-30
    [Name] => Stewie
    [email= => stewie@loverschoice.tv] => [email=stewie@loverschoice.tv]stewie@loverschoice.tv
    [Title] => Wow
    [Location] => Toronto, Canada
    [Rating] => 5
    [Review] => This is the Greatest product I have ever used!
    [Approval] => 1
    [ProductID] => 3
    [RecordClassName] => Review
    )

    [components:protected] =>
    [brokenOnWrite:protected] =>
    [brokenOnDelete:protected] =>
    [componentCache:protected] =>
    [iteratorPos:protected] =>
    [iteratorTotalItems:protected] =>
    [failover:protected] =>
    [_object_cache:protected] => Array
    (
    )

    [_xml_cache:protected] => Array
    (
    )

    [_natural_cache:protected] => Array
    (
    )

    [customisedObj:protected] =>
    [parent:protected] =>
    [namedAs:protected] =>
    [class] => Review
    [extension_instances:protected] => Array
    (
    )

    )

    [1] => Review Object
    (
    [destroyed] =>
    [record:protected] => Array
    (
    [ID] => 4
    [ClassName] => Review
    [Created] => 2009-07-30 13:00:40
    [LastEdited] => 2009-07-30 13:00:40
    [Date] => 2009-07-22
    [Name] => Tammy
    [email= => tammy@bus.com] => [email=tammy@bus.com]tammy@bus.com
    [Title] => Hey Now
    [Location] => Toronto, Canada
    [Rating] => 3
    [Review] => Hey Now
    [Approval] => 1
    [ProductID] => 3
    [RecordClassName] => Review
    )

    [changed:protected] => Array
    (
    )

    [original:protected] => Array
    (
    [ID] => 4
    [ClassName] => Review
    [Created] => 2009-07-30 13:00:40
    [LastEdited] => 2009-07-30 13:00:40
    [Date] => 2009-07-22
    [Name] => Tammy
    [email= => tammy@bus.com] => [email=tammy@bus.com]tammy@bus.com
    [Title] => Hey Now
    [Location] => Toronto, Canada
    [Rating] => 3
    [Review] => Hey Now
    [Approval] => 1
    [ProductID] => 3
    [RecordClassName] => Review
    )

    [components:protected] =>
    [brokenOnWrite:protected] =>
    [brokenOnDelete:protected] =>
    [componentCache:protected] =>
    [iteratorPos:protected] =>
    [iteratorTotalItems:protected] =>
    [failover:protected] =>
    [_object_cache:protected] => Array
    (
    )

    [_xml_cache:protected] => Array
    (
    )

    [_natural_cache:protected] => Array
    (
    )

    [customisedObj:protected] =>
    [parent:protected] =>
    [namedAs:protected] =>
    [class] => Review
    [extension_instances:protected] => Array
    (
    )

    )

    )

    [odd:protected] => 0
    [first:protected] => 1
    [last:protected] =>
    [current:protected] =>
    [pageStart:protected] =>
    [pageLength:protected] =>
    [totalSize:protected] =>
    [paginationGetVar:protected] => start
    [iteratorPos:protected] =>
    [iteratorTotalItems:protected] =>
    [failover:protected] =>
    [_object_cache:protected] => Array
    (
    )

    [_xml_cache:protected] => Array
    (
    )

    [_natural_cache:protected] => Array
    (
    )

    [customisedObj:protected] =>
    [parent:protected] =>
    [namedAs:protected] =>
    [class] => DataObjectSet
    [extension_instances:protected] => Array
    (
    )

    )

    )

    I can't seem to drill past the ReviewObject using

    array($reviewContent[0])

    ...

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: DataObject::get method Link to this post

    Not sure why you want to nest the dataobject get call in an array, thats a bit weird

    $records = DataObject::get("Review","Approval = 1 AND ProductID =" . $this->ID);

    $records will have all the info you need as a dataobjectset. Because it returns a dataobjectset you can iterate, find, filter everything through that.

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