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
#2

(This post was last modified: 05-19-2017, 09:45 AM by Kaosweaver.)

Jay,
I have no idea why it isn't working (it should, I went and tracked down the custom_result_object code and it is solid).

The shortlink_list class's __set isn't being called (at least that's what my quick test revealed) I put echo/die() in the __set before anything else and it never triggered, so, protected/private vars are assigned from and the __set is ignored.

The workaround I did on it was to move the assignment line in to the timestamp function, like so:


PHP Code:
public function timestamp($format)
   {
       $this->timestamp DateTime::createFromFormat('U'$this->timestamp);
       return $this->timestamp->format($format);
   

I'm very certain there is a better way to do that, but that's the quick and dirty to get what you're looking for in the mean time.
Reply
#3

In your class "Shortlink_list" remove this:

PHP Code:
protected $timestamp

Then the class will call the __set and the correct value will populate. (did for me). So you can remove the setting the value in the timestamp function.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB