Access Model custom record objects
Hi, how can I access to custom record class?
The model of a simple class is like:
class Examples_model extends MY_Model {
public $required = array('name');
function __construct()
{
parent::__construct('example'); //table name
}
}
class Example_model extends MY_Model {
Custom record model methods go here....
function register()
{
}
}
If I want to access to register function, how can I do it from the controller? Do I have to call the function from the parent model?
Comments
$my_example = $this->examples_model->find_one(array('id' => 1)); $my_example->register();
More can be read here:
http://docs.getfuelcms.com/general/models#custom_records
Due I'm going to use the custom properties in many functions within the controller I did this:
public $user_record; public function __construct() { parent::__construct(); $this->load->library('form_validation'); //load my custom forms $this->load->config('forms'); //load models $this->load->model('portal_users_model'); $this->user_record = $this->portal_users_model->find_one(array('id' => 1)); } public function register(){ /* code */ $this->user_record->register($user); }
But I have another question, this is more about some concepts or the purpose of some classes of fuel cms.
At the beginning, when I was going to use my register function, I thought that it has to be in the class that extends base_module_record because the user guide say that the purpose of this is class is for custom record functions. If my register function is not for neither fuel users table nor admin/dashboard, I thought that it was a custom function.
As you can imagine, when I test my function register
public function register($user){ $this->db->insert('portal_users', $user); if($this->db->affected_rows() == '1'){ return $this->db->insert_id(); } return FALSE; }
This didn't work, because this class extends Data Record class, and this don't use any of the functions that I was calling in my function register().
So, if I want to use that function, it has to be in the class that extends either base_module_model or MY_model.
My main question, in a model, what functions should I put in the class that extends base_module_model and what functions in the class that extends base_module_record?
P.S Now that I read some of the documentation of my_model class, I delete my register function, and now I'm using insert() from my_model class.
function get_name() { return $this->first_name.' '.$this->last_name; }
If you want to overwrite a property with your own custom value, say "first_name", you'll need to use the _fields property like so:
function get_first_name() { return 'Mr. '.$this->_fields['first_name']; }