Jump to:

23480 Posts in 18951 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » [solved] Formatting a field within an array.

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

Page: 1
Go to End
Author Topic: 530 Views
  • cumquat
    Avatar
    Community Member
    198 Posts

    [solved] Formatting a field within an array. Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: [solved] Formatting a field within an array. Link to this post

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

  • cumquat
    Avatar
    Community Member
    198 Posts

    Re: [solved] Formatting a field within an array. Link to this post

    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

    530 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.