CodeIgniter Forums

Full Version: Extend Active Record Class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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();

El Forum

[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.

El Forum

[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?

El Forum

[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';