PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 4, 2022

[FIXED] How can I load model to helper?

 March 04, 2022     codeigniter     No comments   

Issue

How can I load model to helper? I need to load it outside of functions, but use them in functions.


Solution

You could get a reference to the controller object and access the model through that.

function my_helper()
{
    // Get a reference to the controller object
    $CI = get_instance();

    // You may need to load the model if it hasn't been pre-loaded
    $CI->load->model('my_model');

    // Call a function of the model
    $CI->my_model->do_something();
}

Another option is to pass the model in when calling the helper function.

function my_helper($my_model)
{
    $my_model->do_something();
}

function my_controller_action()
{
    // Call the helper function, passing in the model
    my_helper($this->my_model);
}


Answered By - Stephen Curran
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing