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 /

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

How to pass custom array to dataObject without updating database?

Go to End

5 Posts   921 Views


Community Member, 11 Posts

5 August 2013 at 8:38pm

Edited: 05/08/2013 8:47pm


I have a problem. I'm saving outclicks on products, to show them on the site reports section in the CMS. The only problem is, I can't seem to figure out how to pass the array to the sourceRecords() in extended SS_Report, without using a DataObject.

I tried to create a DataObject for the results, but it keeps creating new ones.

I have 2 objects/databases, 1 is the clicks where there are many ProductID's, the other one is the object/database where I pull the counted clicks, so the ProductID's there should be unique.

How can I UPDATE an object, with a specific ID, rather then it adding a new one?

I've tried using the ->write(), but that only creates a new one. Also using ->push() doesn't work. What should I do?

PS. To make it even more clear:


	static $db = array(
	public static $has_one = array(
			"Product" => "Product"


In between here, I collect all the above ProductOutclicks, and merge them into their respective ProductID, counting the total amount of clicks.



static $db = array(
		"Name" => "Text",
		"Clicks" => "Int",
		"ProductsID" => "Int"

Now I want to pull a specific ProductID from this object and UPDATE it, if the ProductID does not work, we add it to the DataObject.

I've tried all kinds of stuff,
custom code

Thanks in advance,
Andreas Saarva.


Forum Moderator, 1307 Posts

5 August 2013 at 9:04pm

$myObject>write() should update the object if you make sure $myObject->ID is set to the right ID first. If the ID is left empty, a new object is inserted.


Community Member, 11 Posts

5 August 2013 at 9:05pm

Edited: 05/08/2013 9:12pm

These are my 2 functions:

	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
		$dataObject = DataObject::get("ProductOutclicks","","ProductID");
		$items = array();
		foreach($dataObject as $do) {
			$Name = DataObject::get_by_id("Product",$do->ProductID);
			$do->setField("Name", $Name->Name);
			if(!isset($items['items'][$do->ProductID]['Clicks']) && $items['items'][$do->ProductID]['Clicks'] <= 0) {
				$items['items'][$do->ProductID]['ProductID'] = $do->ProductID;
				$items['items'][$do->ProductID]['Clicks'] = 1;
				$items['items'][$do->ProductID]['Name'] = $Name->Name;
			else {
		// Then we save all the new statistics
		foreach($items['items'] as $id => $item) {
			$check = DataObject::get_by_id("ProductOutclickReportObject",$id);
			$check->Name = $item['Name'];
			$check->Clicks = $item['Clicks'];
			$check->ProductID = $item['ProductID'];

		return DataObject::get("ProductOutclickReportObject");


Community Member, 11 Posts

5 August 2013 at 9:11pm

Fatal error: Call to undefined method stdClass::write() in /wwwsf/exam/mysite/code/ProductOutclickReport.php on line 63

It doesn't seem to like the ->write()


Community Member, 11 Posts

6 August 2013 at 3:33am