Hi,
I created the following library, meant to be used in models so query results can be returned in different formats:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
# /application/libraries/Return_object.php
Class Return_object
{
private $result;
private $ci;
public function __construct()
{
$this->ci = &get_instance();
$this->result = new StdClass();
$this->result->data = null;
$this->result->status = false;
$this->result->count = 0;
$this->result->feedback = '';
}
public function output($data=null, $response_type='', $params=array())
{
// Get the data property
if($data && is_object($data))
{
$this->result->status = true;
if($data->num_rows() > 0)
{
$this->result->feedback = $this->ci->db->last_query();
$this->result->data = $data->result_object();
$this->result->count = $data->num_rows();
}
elseif($data->num_rows == 0)
{
$this->result->feedback = 'Nothing returned';
}
}
// Set the callback (note: if JSONP without callback requested, an error will already be thrown by sanitize)
$callback = isset($params['callback']) ? $params['callback'] : null;
// Handle response type and return object
$response_type = strtoupper($response_type);
switch ($response_type)
{
case 'JSON':
return json_encode($this->result);
break;
case 'JSONP': // JSON with Padding, to overcome cross-domain restrictions
return $callback . '(' . json_encode($this->result) . ');';
break;
case 'ARRAY':
return (array) $this->result;
break;
default:
return $this->result;
}
}
public function __destruct()
{
unset($this->result);
}
}
So, I use the library in models like this:
First model: ra_promotors_model.php
PHP Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
# /application/models/Ra_promotors_model.php
class Ra_promotors_model extends CI_Model
{
private $table = 'ra_promotors';
public function __construct()
{
parent::__construct();
$this->load->database('assets');
$this->load->library('return_object');
}
public function get()
{
$query = $this->db->get($this->table);
return $this->return_object->output($query);
}
}
?>
Second model: ra_users_model.php
PHP Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
# /application/models/Ra_users_model.php
class Ra_users_model extends CI_Model
{
private $table = 'ra_users';
public function __construct()
{
parent::__construct();
$this->load->database('assets');
$this->load->library('return_object');
}
public function get()
{
$query = $this->db->get($this->table);
return $this->return_object->output($query);
}
}
?>
In a controller the model now returns a neat object, with a data property, a status property and as feedback, the last performed query...
Code:
$this->load->model('ra_promotors', 'rap_model');
$temp['promotors'] = $this->rap_model->get();
Gives the following result in the browser (see attached image)
Everything works fine until two models come into play:
Code:
$this->load->model('ra_promotors', 'rap_model');
$temp['promotors'] = $this->rap_model->get();
$this->load->model('ra_users', 'rau_model');
$temp['users'] = $this->rau_model->get();
Now, the two parts of the array are displayed BUT I get the result of the 'ra_promors_model' twice!!!
If I do NOT USE the return object library, I get the two different query results - as expected-...
So it has something to do with the library I created :-(
Anyone?
Thanks for the help!
Zelf