Access Model custom record objects

edited January 2014 in Modules
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

  • edited 11:49PM
    The record class model should extend the "Data_record" class or the "Base_module_record" if you are creating a simple module. The table class models should extend "MY_Model" or "Base_module_model" (if you are creating a simple module). Then to access the custom property you'd do something like the following:
    $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
  • edited 11:49PM
    Thank you so much, that's what I need.

    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); }
  • edited 11:49PM
    Well... I should read MY Model Class before... so I didn't have to do the register function when my model class has one.

    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.
  • edited 11:49PM
    Think of the Table class (MY_Model) as a way to return one or more record classes. Put methods on your record class that you think make sense at the record level. One example is if you have a first_name and a last_name field, you could create a method like so:
    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']; }
  • edited 11:49PM
    Thank you for the explanation, and sorry for my bad english, but english is not my main language
Sign In or Register to comment.