21489 Posts in 5783 Topics by 2622 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 335 Views |
-
[solved] Formatting a field within an array.

26 September 2011 at 10:58pm
Hi there,
I maybe going about this the wrong way of have the wrong end of the stick but i have an SQL query that pulls data from a table on another SilverStripe database, this is all fine and pulls the data through great. The one thing i need to do is to formate the $CalloutDate on the template to be Nice. On the website where i am pulling the data from, the code
public function getNiceDate() {
return $this->obj('CalloutDate')->Nice();
}works fine, but that's because it is referencing the dataobject on that page, looking at the code below I believe that the SQL query creates an array which i then turn into a dataset but all the data would just be a plain string, does this seem right?
public function CantechCallouts() {
global $databaseConfig, $databaseConfigCantech;
DB::connect($databaseConfigCantech);
$sqlQuery = new SQLQuery();
$sqlQuery->select = array('CalloutDate','Location','Outcome');
$sqlQuery->from = array("Callout");
//$sqlQuery->where = "";
$sqlQuery->orderby = "CalloutDate desc";
$rawSQL = $sqlQuery->sql();
$result = $sqlQuery->execute();
$dataObject1 = new DataObjectSet();
foreach($result as $row) {
//move the SQL result data to the DataObjectSet
$dataObject1->push(new ArrayData($row));
}
DB::connect($databaseConfig);
return $dataObject1;
}So what i want to do is use $CalloutDate.Nice in the template.
any help or pointers?
regards
Mick
-
Re: [solved] Formatting a field within an array.

27 September 2011 at 7:38pm
To be able to call Nice() you need to wrap the CalloutDate value you pass into your ArrayData into a SS_Datetime object.
$date = new SS_Datetime();
$date->setValue($somevalue..);$set->push(new ArrayData(array(
..
'CalloutDate' => $date
))); -
Re: [solved] Formatting a field within an array.

28 September 2011 at 8:52pm Last edited: 28 September 2011 8:53pm
Hi Willr,
Thanks for your help, i didn't fully understand the code to be honest but it did lead me to another post you helped a guy on (which i didn't find when i searched first time) and then it all jumped into place. The post that i found using your comments below was http://www.silverstripe.org/template-questions/show/14058
my final code now looks like this for anyone else stuck here.
public function CantechCallouts() {
global $databaseConfig, $databaseConfigCantech;
DB::connect($databaseConfigCantech);
$sqlQuery = new SQLQuery();
$sqlQuery->select = array('*');
$sqlQuery->from = array("Callout");
//$sqlQuery->where = "";
$sqlQuery->orderby = "CalloutDate desc";
$rawSQL = $sqlQuery->sql();
$result = $sqlQuery->execute();
$dataObject1 = new DataObjectSet();
foreach($result as $row) {
//move the SQL result data to the DataObjectSet
$dataObject1->push(new ArrayData(array(
'CalloutDate' => DBField::create('Date', $row['CalloutDate']),
'Type' => $row['Type'],
'Age' => $row['Age'],
'Sex' => $row['Sex'],
'Outcome' => $row['Outcome'],
'TOD' => $row['TOD'],
'Location' => $row['Location'],
'Duration' => $row['Duration']
)));
}
DB::connect($databaseConfig);
return $dataObject1;
}Mick
| 335 Views | ||
|
Page:
1
|
Go to Top |


