CodeIgniter Forums
Extend Active Record Class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Extend Active Record Class (/showthread.php?tid=8998)



Extend Active Record Class - El Forum - 06-08-2008

[eluser]stoefln[/eluser]
Is it possible to write my own container class for the data?
i mean somehow this way:

Code:
$query = $this->db->get_where('types',array('id' => $typeId));
$type = $types[0];
$type->myUserMethod();



Extend Active Record Class - El Forum - 06-08-2008

[eluser]m4rw3r[/eluser]
No, but you can fetch the data and then pass it to a container:
Code:
$query = $this->db->get_where('types',array('id' => $typeId));
$row = $query->row_array();
$type = new Type($row);
$type->myUserMethod();

EDIT: My IgnitedRecord model has support for custom container classes.


Extend Active Record Class - El Forum - 06-09-2008

[eluser]stoefln[/eluser]
[quote author="m4rw3r" date="1212948856"]No, but you can fetch the data and then pass it to a container:
Code:
$query = $this->db->get_where('types',array('id' => $typeId));
$row = $query->row_array();
$type = new Type($row);
$type->myUserMethod();

EDIT: My IgnitedRecord model has support for custom container classes.[/quote]

thanks, nice proposal!
the problem is that on huge result sets there have to be twice as much instances made as without the wrapper objects, think that could be a performance-issue.
anyway, your ignitedrecord model looks quite impressive, will take a closer look on it.

how would you include the Type.php in the typesmodel.php file, is there a CI-style-way of achieving that?


Extend Active Record Class - El Forum - 06-09-2008

[eluser]m4rw3r[/eluser]
This:
Code:
class Type{
    function Type($data = array()){
        foreach($data as $key => $val){
            $this->$key = $val;
        }
    }
}
$query = $this->db->get_where('types',array('id' => $typeId));
$row = $query->row_array();
$type = new Type($row);
is actually marginally faster than this:
Code:
class Type{

}
$query = $this->db->get_where('types',array('id' => $typeId));
$type = mysql_fetch_object($query->result_id,'Type'));
The lower one requires MySQL and PHP 5 or higher, so I think you should use the first one.

About the includes: There is no "CI way" of including associated classes, not yet at least.
But do what you feel is right, include, require (or the *_once), $this->load->library(...) or whatever.
I recommend using the APPPATH constant (yes three PConfused, it is the path to your application folder) in your includes/requires, like this:
Code:
require_once APPPATH.'/myclassdir/type.php';