Welcome Guest, Not a member yet? Register   Sign In
Using custom_result_object
#1

Hi!

I'm trying to figure out how to use the "custom_result_object" and I need something which is very similar to the example in the documentation. The date is stored in a table as a unix timestamp and would like to have it returned as a human readable date.

https://codeigniter.com/user_guide/datab...lt-objects

So I figured I'd give "custom_result_object" a try because it could come in handy in some other spots.

Model:

PHP Code:
public function get_shortlink_list($team 0$order 0$limit 0$offset 0) {
 
       $this->db->where('owner',$this->session->usr_id);

 
       if($order)
 
           $this->db->order_by($order);

 
       if($limit)
 
           $this->db->limit($limit,$offset);

 
       $data $this->db->get('entries');

 
       if($data->num_rows() > 0) {
 
           require(APPPATH.'/models/custom_results/Shortlink_List.php');

 
           return $data->custom_result_object('Shortlink_List');
 
       }

 
       return false;
 
   

The Shortlink_List.php contains:


PHP Code:
<?php

defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Shortlink_list {

 
   public $id;
 
   public $url;
 
   public $custom;

 
   protected $timestamp;

 
   public function timestamp($format)
 
   {
 
       return $this->timestamp->format($format);
 
   }

 
   public function __set($name$value)
 
   {
 
       if ($name === 'timestamp')
 
       {
 
           $this->timestamp DateTime::createFromFormat('U'$value);
 
       }
 
   }

 
   public function __get($name)
 
   {
 
       if (isset($this->$name))
 
       {
 
           return $this->$name;
 
       }
 
   }


The part of the controller using this:


PHP Code:
public function links() {
 
       
        $result 
$this->shortlink->get_shortlink_list();
 
       
        foreach
($result as $row) {
 
           echo $row->id.':';
 
           echo $row->timestamp('Y-m-d').'<br />';

 
       }

 
   

The result I'm getting is: 

Fatal error: Call to a member function format() on string in

Even though I've followed the example in the documentation very closely. Any ideas / hints would be appricated.

Thanks,

jay
Reply


Messages In This Thread
Using custom_result_object - by thejay - 05-18-2017, 08:47 AM
RE: Using custom_result_object - by Kaosweaver - 05-19-2017, 09:44 AM
RE: Using custom_result_object - by Kaosweaver - 05-19-2017, 10:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB