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

Transforming a SQLQuery() result to DataObjectSet


Go to End


6 Posts   2151 Views

Avatar
iON creative

Community Member, 42 Posts

27 June 2010 at 3:33pm

I'm having difficulty returning a DataObjectSet result from an SQLQuery. I've been following the docu page outline and reviewing other suggestions from the forum, but I'm consistently getting errors.

My function is:

function OpenhousesSaturday()
   {

	$week_number = date('W');
	$year = date('Y');
	$day =  date("Y-m-d", strtotime($year."W".$week_number."6"));

	$sqlQuery = new SQLQuery();

	$sqlQuery->select = array(
      '`Openhouse`.*',
      '`PropertyPage`.*',
	);

    $sqlQuery->from = array("Openhouse", "LEFT JOIN `PropertyPage` ON `Openhouse`.`PropertyPageID` = `PropertyPage`.`ID`");
    $sqlQuery->where = array("'$day' = `Openhouse`.`ViewDate`");

      // get the raw SQL
      	$rawSQL = $sqlQuery->sql();
      // execute and return a Query-object
      	$result = $sqlQuery->execute();
 
 	  //setup our blank DataObjectSet to push SQL result data into it.
     	 $dataObject1 = new DataObjectSet();
		  foreach($result as $row) {
			 //move the SQL result data to the DataObjectSet
			  $dataObject1->push(new ArrayData($row));
		  }
		  
      return $dataObject1; 
} 

And I'm getting this error:
Fatal error: Call to a member function XML_val() on a non-object...

Can anyone point me in the right direction on this?

Many thanks,

Jayne

Avatar
biapar

Forum Moderator, 435 Posts

27 June 2010 at 9:11pm

Avatar
iON creative

Community Member, 42 Posts

27 June 2010 at 10:45pm

Thanks for that. I went through that and a few other resources and I'm still quite stuck.

Avatar
Willr

Forum Moderator, 5523 Posts

28 June 2010 at 9:19am

I'm not sure but looking at the 2.4 API the $sqlQuery->from() should only take a string eg 'Openhouse' and you should use $sqlQuery->leftJoin() to add the join.

I'm not vastly experienced with using custom SQLs but you could rewrite that as a dataobject get you would lose the Openhouse data but as far as I can see you would only want it for the date.

DataObject::get('PropertyPage', "Openhouse.ViewDate = '$date'", "", "LEFT JOIN Openhouse ON PropertyPage.ID  = Openhouse.PropertyPageID");

Avatar
iON creative

Community Member, 42 Posts

28 June 2010 at 9:22am

Thanks Willr - this site is running 2.3.7, but I'll give that a try. And the reason we're using SQLQuery is that we need to display the Openhouse data as well. It's my first foray into SQLQuery! I'll provide an update later today. Thanks again, Jayne.

Avatar
biapar

Forum Moderator, 435 Posts

28 June 2010 at 10:09pm

I think that with SQLQuery is possible to create a complicate query and thus is not possible with dataobject:get...Is possible to have more examples on SQLQuery use? ( We have already read SQLQuery page doc. I that page there is error on sqlquery syntax )...