Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Custom object from DB result (Where/How to put/load class?)
#1

[eluser]J. Pavel Espinal[/eluser]
Hello,

In the CodeIgniter User Guide, "Database Library" documentation, section "Generating Query Results", result() method documentation, it says:

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)

Code:
$query = $this->db->query("SELECT * FROM users;");

foreach ($query->result('User') as $row)
{
   echo $row->name; // call attributes
   echo $row->reverse_name(); // or methods defined on the 'User' class
}


The questions are:
a. How should that class be "loaded"?
as a core library?
Code:
$this->load->library('class_name', $config, 'object name')

as a model?
Code:
$this->load->model('Model_name');

or as a generic file (and not sending output to the browser)?
Code:
$this->load->file('filepath/filename', true/false)



Please pardon my ignorance regarding this subject,

Thanks in advance.
#2

[eluser]Aken[/eluser]
You can't use $this->load->file(), as that will open the file and read / return its contents, not just include it.

I would just use old fashioned include() or include_once() to make sure your class is available. I wouldn't want to use a library or model, since it'll assign stuff to the CI object that you don't need.
#3

[eluser]J. Pavel Espinal[/eluser]
Aken, thanks for your answer.

I think I'll use that approach (include/include_once) as there's no seems to be a native CI's way to load a 'generic class'.


Best regards,




Theme © iAndrew 2016 - Forum software by © MyBB