CodeIgniter Forums
Extending the native model class but having problems with the constructor - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Extending the native model class but having problems with the constructor (/showthread.php?tid=31055)



Extending the native model class but having problems with the constructor - El Forum - 06-04-2010

[eluser]Unknown[/eluser]
Hi there,

I'm trying to extend the native model class so that it has a default method that gets all the data from the db table of the same name as the model. The problem is that any code that i put in the constructor doesn't seem to run, although i have seen examples of using the constructor in an extended native class on the web. Can anyone help?

Code:
class MY_Model extends Model {

    var $tableName;
    
    function MY_Model() {
     parent::Model();
     $modelName = explode('_model',get_class($this));        
     $this->tableName = strtolower($modelName[0]);
    }

    function getAll($sortData = array(),$fields = array()) {
                
     if(!empty($sortData)) {
     $this->db->order_by($sortData['sort_field'],$sortData['sort_direction']);            
     }
     if(!empty($fields)) {
        $this->db->select($fields);
     }
     return $this->db->get($this->tableName)->result_array();                
   }
}

thanks!!
jk.