Welcome Guest, Not a member yet? Register   Sign In
Query results - passing a string
#1

(This post was last modified: 08-22-2015, 06:25 AM by groovebird.)

Hi,

i found this on die CI-Documentation: (http://www.codeigniter.com/userguide3/da...sults.html)

Quote:You can also pass a string to result() which represents a class to instantiate for each result object (note: this class must be loaded)

My Question: Where do i have to put this class (in which folder)? What is the code for such a class and what is the difference to a model? Do i have to create a seperate model class? I cant find anything in the documentation. A code sample would be great :-)
Reply
#2

Create a library and load it. Then this should work: http://www.codeigniter.com/userguide3/da...lt-objects
Reply
#3

Thank you.

I thougt there is another option, because for me such a result object class is not a library. What also works is writing (the last_login method from your example) in the model but i think this is not the recommended way. Then i don't need another class.
Reply
#4

You can create the class as a model if you wish, or as any type of class which you can load (so it could be a class loaded by Composer, for example). If you use the mysqli driver, the underlying code uses mysqli_result::fetch_object(), so the PHP manual may be of some use: http://www.php.net/manual/en/mysqli-resu...object.php
Reply
#5

Hi,

does anybody know how this custom_result_objects work with a join of tables?
Lets say i have a users and location table. As a result i get the user-object. Then i have access to my user->get_fullname()-method but i have no location object and no access to any methods in my location model.

My idea was to write the following method in my user model:

PHP Code:
function location() {
  
//some pseudo code
  // Select location where location id = $this->location_id


and in the controller:
PHP Code:
$this->user->location->get_name(); 

BUT if the result has 100 Users, then i execute 100 queries to get the location objects and thats not really what i want :-) I think it works for a single user but a find_all-method would be not so good.
Reply
#6

I believe it works fine with joins, you just have to make sure that all of the selected fields have unique names (ignoring the table names) or aliases. It may even work without unique names, but I have no idea how predictable the outcome would be in that case.
Reply
#7

yes that works fine, but i dont know how to use this get-methods. In this methods i can format the output of some values. now i have only the raw value from the db.
Reply
#8

You can add a __set() magic method to your class to format the data when the object is originally built. A __get() method (or any other method used to retrieve data from the class) will deal with the data as it is stored in the object, which can be useful if you need to change it for a given use, but, in most cases, you just want to convert the data from the database format to a more useable format for PHP when the object is originally created.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB