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.

General Questions /

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

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

[solved] Formatting a field within an array.


Reply


3 Posts   568 Views

Avatar
cumquat

Community Member, 198 Posts

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

Avatar
Willr

Forum Moderator, 5513 Posts

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
)));

Avatar
cumquat

Community Member, 198 Posts

28 September 2011 at 8:52pm

Edited: 28/09/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