Welcome Guest, Not a member yet? Register   Sign In
Modelling Questions
#2

[eluser]tonanbarbarian[/eluser]
I simply create a method in my model called load which you then call to get the record
Well actually I create a class that extends the core model and put the method in there so that all of my models can use the code

Code:
libraries/My_Model.php
class MY_Model extends CI_Model {

  var $_table = null;
  var $_key = 'id';

  functon MY_Model() {
    parent::CI_Model();
  } // MY_Model()

  function load($id) {
    if (empty($this->_table) OR empty($this->_key) OR empty($id))
      return false;
    $query = $this->db->getfrom($this->_table, $this->_key .'='. (int)$id);
    return $query->row(); // or return $query->row_array(); if you want the result as an array
  } // load()

}

Then the actual model would be
models/person.php
Code:
class Person extends MY_Model {
  var $_table = 'person';
  var $_id = 'id';

  // constructor
  function Person() {
    parent::MY_Model();
  } // Person()

}

then to instantiate in your controller you do
Code:
...
$this->load->model('person');
$data = $this->person->load(3);
...


Messages In This Thread
Modelling Questions - by El Forum - 11-29-2007, 12:14 PM
Modelling Questions - by El Forum - 11-29-2007, 01:44 PM
Modelling Questions - by El Forum - 12-01-2007, 10:19 AM
Modelling Questions - by El Forum - 12-01-2007, 11:35 PM
Modelling Questions - by El Forum - 12-02-2007, 09:42 PM
Modelling Questions - by El Forum - 12-02-2007, 11:40 PM
Modelling Questions - by El Forum - 12-03-2007, 12:28 AM
Modelling Questions - by El Forum - 12-04-2007, 07:55 PM
Modelling Questions - by El Forum - 12-04-2007, 11:11 PM
Modelling Questions - by El Forum - 12-04-2007, 11:55 PM
Modelling Questions - by El Forum - 12-05-2007, 12:39 AM
Modelling Questions - by El Forum - 11-30-2008, 07:11 AM
Modelling Questions - by El Forum - 11-30-2008, 11:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB