Simon Perepelitsa
Simon Perepelitsa

Reputation: 20639

How to load helper from model in CodeIgniter?

I want to load some helper in a model. How to do this? Tried to use:

${get_parent_class($this)}->load->helper ('text');

But still getting an error

Fatal error: Call to a member function helper() on a non-object

Upvotes: 8

Views: 31914

Answers (4)

oparam
oparam

Reputation: 209

You are not need to load helper in a model.Just load helper in a controller and use function in a model as well as we normally use helper function in a controller

Upvotes: 0

mwm
mwm

Reputation: 1973

GSto answered $this->load->helper('helpername') but if you are in a model's method, $this simply refers to that model's (class) instance and not to CI global. That won't work!

Instead you need to load the CI global and then load the helper:

// PHP 4
// $ci =& get_instance();
// PHP 5    
$ci = get_instance();
$ci->load->helper('text');

Upvotes: 41

Tudor
Tudor

Reputation: 1181

I think CI doesnt check for helper duplication ... CI herlpers are procedural files , you might include ur helper twice if ur controller has the same helper loaded as ur model (that is loaded in that controller). Maybe do a library instead...

I can see i get negative votes w/o any comments ... by checking loader class from core CI u can see helpers method isnt checking if the helper has been loaded before (it isn't included in is_loaded() array like most classes that are loaded through load factory class) ... I dont recommend anyway to load helpers in both models and controllers ... for ex i made a helper for output encoding that i use in controllers (before i pass data to the view) . It would be very bad if i change the view state twice ...

Upvotes: -3

GSto
GSto

Reputation: 42350

$this->load->helper('helpername')

Upvotes: -2

Related Questions