Welcome Guest, Not a member yet? Register   Sign In
Attention Expert users: What are the things you wish you knew about CI when you started?
#10

[eluser]gtech[/eluser]
Retrieving Models From A View

I had some issues with MVC and thought that you could not call a model method from a view (I was quite adamant this was incorrect). However MVC suggests you can as long as you do not alter the models data. An example of when this might be useful is if multiple controllers call the same view, you will not have to access the models method in every controller to retrieve the models method data.

Calling Other Model Methods Within Models

You can also call other models methods from a model. An example might be a customer model method might need to access a method from the user model. to do this you can load the user model in the customer models constructor.

Code:
class Customersdb extends Model
{
  /**
   * The Initialisation method
   */

  function Customersdb()
  {
    parent::Model();
    $this->load->model('usersdb');
  }

  function removeCustomer($id)
  {
    $query = $this->usersdb->removeCustomerUsers($id));
    ...

or use the CI instance which means you only need to load the model when the correct method is called.

Code:
class Customersdb extends Model
{
    private $CI;
    
    function Customersdb()
    {
        parent::Model();
        $this->CI =& get_instance();    
    }
    
    function removeCustomer($id)
    {
        $this->CI->load->model('usersdb');    
        $this->CI->usersdb->removeCustomerUsers($id);
        ...
    }    
}
mind you this might of been fixed in 1.6.x


Messages In This Thread
Attention Expert users: What are the things you wish you knew about CI when you started? - by El Forum - 04-17-2008, 05:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB