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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

DataObject::get method


Go to End
Reply


2 Posts   4548 Views

Avatar
zenmonkey

Community Member, 532 Posts

1 August 2009 at 1:02am

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])
...

Avatar
Willr

Forum Moderator, 5514 Posts

1 August 2009 at 9:06pm

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.