result object library for returning Query Builder query results - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: result object library for returning Query Builder query results (/showthread.php?tid=63339) |
result object library for returning Query Builder query results - Zeff - 10-21-2015 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'); 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'); Second model: ra_users_model.php PHP Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 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'); Everything works fine until two models come into play: Code: $this->load->model('ra_promotors', 'rap_model'); 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 RE: result object library for returning Query Builder query results - mwhitney - 10-21-2015 When you load the library, if it has already been loaded previously, the existing instance of the library is returned, and the constructor is not executed again. In both cases, you return $this->result, which is a property which has been assigned an instance of stdClass. Since this is a class, when you assign the result of $this->return_object->output($query) to a variable (or, in your case, an index in an array), you're creating a reference to the instance of $this->result in $this->return_object. Honestly, I'm not 100% sure why you would get the first one twice, rather than the second one twice, unless the second one didn't return a result. You should be able to fix this by using `clone` when returning $this->result from output(), or by defining $this->result as an array, then casting it to an object as needed. You may also want to reset $this->result in output(), since anything you don't set will still be set to the value from the previous call, so, currently: - if you pass a falsey value for $data, everything in $this->result will be from the previous call - if you pass a truthy object for $data, but num_rows() is less than or equal to 0 for some reason, $this->result->data and $this->result->count will be from the previous call Also, note that last_query() doesn't work if the current database configuration has save_queries disabled. RE: result object library for returning Query Builder query results - Zeff - 10-22-2015 (10-21-2015, 12:43 PM)mwhitney Wrote: When you load the library, if it has already been loaded previously, the existing instance of the library is returned, and the constructor is not executed again. Hello mwhitney, Thanks for your answer. I thought, each time a model is loaded, a separate instance of return_object 'lives' in that model (so the constructor of this class is loaded for each instance)... It seems I will have to enhance my CI-related OOP knowledge Kind regards, Zelf RE: result object library for returning Query Builder query results - mwhitney - 10-26-2015 It can be a bit tricky at times, but it really comes down to the CI_Controller being a singleton and setting up some simple shortcuts to the CI_Controller instance. The CI_Controller singleton is setup like this (some comments and whitespace removed to trim down the code): PHP Code: class CI_Controller { To ensure the same instance is retrieved in most contexts, CodeIgniter.php defines the global get_instance() function as follows: PHP Code: // Load the base controller class When you load a model or library, the loader gets the CI_Controller instance, then checks whether the model/library has already been loaded on that instance. If it has already been loaded, the loader returns without doing anything else, unless you supply an alternate name in the call to the loader. If it has not been loaded, or you supply an alternate name for the library/model, it loads the library/model and assigns it to the CI_Controller instance as a property named with the name of the library/model or the alternate name. The bulk of the code in CI_Model is a magic method to access the CI_Controller instance if a method or property isn't defined on the model: PHP Code: class CI_Model { |