Where to put and how to load the class of the result object in Codeigniter?

I am looking for a suitable way to organize "Object Classes of Objects" in CodeIgniter. These classes are commonly used in models, as described in the documentation :

You can also pass the result () string, which represents the class for the instance for each result object (note: this class must be loaded)

$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
}

So, where to put the class "User" and is there any "official" way to load it?

+5
source share
2 answers

Try using the library.

Put your class in the application / library folder, then use the bootloader to load it:

$this->load->library('user');
+2
source

, , MY_Model :

public function merge_object(&$object, $data){

    if (is_object($data)) {
        $data = get_object_vars($data);
    }
    if (!empty($data)) {
        foreach ($data as $property => $value) {
            if (!property_exists($object, $property)) {
                $object->$property = $value;
            }
        }
    }
    return $object;
}

$this- > merge_object ($ this, $data); __construct ($ data = array())

, , . , .

0

All Articles