CodeIgniter Forums

Full Version: Using custom_result_object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

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.

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


PHP Code:
public function get_shortlink_list($team 0$order 0$limit 0$offset 0) {



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

       if($data->num_rows() > 0) {

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

       return false;

The Shortlink_List.php contains:

PHP Code:

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

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 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.


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.
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.