• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Query results - passing a string


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 :-)

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

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.

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


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:

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.

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.

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.

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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.