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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Pushing a field value into a DataObject::get array?


Go to End


4 Posts   1577 Views

Avatar
Valorez

Community Member, 11 Posts

2 August 2013 at 2:38am

Edited: 02/08/2013 2:39am

Hello there, I recently started using Silverstripe as a part of my new job. It's going pretty good so far, seems like a really powerful tool and I like it alot.

Anyhow, I bumped into a problem which I can not solve (the Silverstripe way, I dont wanna do too much custom coding).

class ProductOutclickReport extends SS_Report {

...

	static function CalculateTime($limit) {
		 $period = 0;
		$temp = explode(" ",trim($limit));
		foreach($temp as $time) {
			if(strstr($time,"m")) {
				$timeMultiplier = 60; // 1 minute
			}
			else if(strstr($time,"h")) {
				$timeMultiplier = 60*60; // 1 hour
			}
			else if(strstr($time,"d")) {
				$timeMultiplier = 60*60*24; // 1 day
			}
			$period = $period + (intval($time)*$timeMultiplier);
		}
		 return $period;
	}

	function sourceRecords($params, $sort, $limit) {
		$calculatedLimit = $this->CalculateTime($params['TimeRange']);
		$time = time();
		$timeFrom = $time - $calculatedLimit;
		$timeTo = $time; // Support for date ranges in later updates
	//	$Products = DataObject::get("ProductOutclicks", "UNIX_TIMESTAMP(Created) >= {$timeFrom} AND UNIX_TIMESTAMP(Created) < {$timeTo}");
		$allProducts = DataObject::get("ProductOutclicks", "UNIX_TIMESTAMP(Created) >= {$timeFrom} AND UNIX_TIMESTAMP(Created) < {$timeTo}");
		foreach($allProducts as $Product) {
			$Product['Name'] = DataObject::get_one("Product","ID = '{$Product->ProductID}'");
			$Products = $Product;
		}
		return $Products;
	}

...

This is a custom report. It records outclicks on products, to be presented in the Site Report section of the admin.

My question is:
How do I get a single row/field using SS functionality, and pushing it to the Product array?

At the moment, when print_r'ing the $Product variable, I get so much information I've never seen before, I dont know how it all works. I guess it's an image if some kind of array-object? Containing [protected]/[private] and alot of stuff... simply not a "normal" array.

Btw, using the commented line, "// $Products = DataObject ..." it works, returning all the affected rows, but the Name is in the Product object, not ProductOutclicks (which has a ProductID).

Thanks in advance,
Andreas Saarva.

Avatar
Valorez

Community Member, 11 Posts

2 August 2013 at 7:43pm

I'm afraid this forum is not too active or atleast well populated with SS developers, does anyone have any suggestions for good forums with other SS developers? :)

Regards,
Andreas.

Avatar
martimiz

Forum Moderator, 1391 Posts

2 August 2013 at 10:35pm

Edited: 02/08/2013 10:36pm

... and the fact that not everyone lives in the same timezone and lots of people may be on vacation :)

But sure, these forums are sometimes quiet, but then again not many questions remain unanswered. But there are other options: check out the irc channel, stackoverflow.com or just google :)

The tutorials in the documentation are really helpful in understanding the SilverStripe data structure - don't skip them!. if you're on 2.4.x, check out: http://doc.silverstripe.org/framework/en/2.4/tutorials/ (in 3.x things are a bit different)

As to your problem:

$Product['Name'] = DataObject::get_one("Product","ID = '{$Product->ProductID}'"); 

DataObkject::get_one() returns a DataObject with all its properties and DataFields, in this case an instance of your Product class, representing one record in the Product datatable. If you want just the Name (if Name is in fact a DataField in Product), you can for instance:

$Product['Name'] = DataObject::get_one("Product","ID = '{$Product->ProductID}'")->Name; 

Also: I find that in SilverStripe, you hardly ever have to turn to arrays to display data: templates, for instance, work with Literals, DataObjects and DataObjectSets only!

Martine

Avatar
Valorez

Community Member, 11 Posts

2 August 2013 at 10:57pm

Edited: 02/08/2013 10:58pm

Hello! :)

Yes, you're probably right about people being on vacation and stuff. I've spent like 6 hours googling and reading the manuals.

I managed to solve it, and I think my question wasn't well formulated. What I wanted to do, is retrieving a DataObject and populate each object with a custom field, kinda like JOIN.

...

	function sourceRecords($params, $sort, $limit) {
		$calculatedLimit = $this->CalculateTime($params['TimeRange']);
		$time = time();
		$timeFrom = $time - $calculatedLimit;
		$timeTo = $time; // Support for date ranges in later updates
		
		$dataObject = DataObject::get("ProductOutclicks");
		
		foreach($dataObject as $do) {
			$do->setField("Name","test");
		}

		return $dataObject;
	}

...

Now it adds a name to each data object. :)

Thanks for your answer!