21278 Posts in 5728 Topics by 2599 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1421 Views |
-
Transforming a SQLQuery() result to DataObjectSet

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
-
Re: Transforming a SQLQuery() result to DataObjectSet

27 June 2010 at 9:11pm
See here: http://doc.silverstripe.org/sqlquery
-
Re: Transforming a SQLQuery() result to DataObjectSet

27 June 2010 at 10:45pm
Thanks for that. I went through that and a few other resources and I'm still quite stuck.
-
Re: Transforming a SQLQuery() result to DataObjectSet

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");
-
Re: Transforming a SQLQuery() result to DataObjectSet

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.
-
Re: Transforming a SQLQuery() result to DataObjectSet

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 )...
| 1421 Views | ||
|
Page:
1
|
Go to Top |

