Welcome Guest, Not a member yet? Register   Sign In
using libraries in models
#1

[eluser]crumpet[/eluser]
Well you can't use libraries in models apparently ... and helpers don't seem to work either... but I want several of my models to use the same functions which I want located in a central place
is there anything like a library which i dont know about
or any way to get around the models not having libraries issue
#2

[eluser]gtech[/eluser]
you can load a model in a model, so you can have a common model if thats what you are after. You can achieve this by loading the 'common' model in the every models constructor that uses the common model, this means it being available in every function.

Or you can get the CI instance in a function and load the model through the CI instance object (there are examples of this in the forum).

you may be also able to extend the common model in the class definition (I must admit I have not tried this approach though).
#3

[eluser]onejaguar[/eluser]
You shouldn't have any problems loading and using helpers or libraries from your model. E.g.

Code:
$this->load->library('image_lib');
  $this->image_lib->initialize();
  $this->load->helper('date_helper');
  mysql_to_unix($time);

Should all work just fine inside one of your model's methods.
#4

[eluser]crumpet[/eluser]
doesn't seem to be working for me
i do
$this->load->library('lib');
$this->lib->libmethod();

and i get error which says somethign like
$lib doesn't exist
can't use libmethod() of a non object...
same for helpers so far..

i'll try the model approach
#5

[eluser]Seppo[/eluser]
Can you specify you PHP version, crumpet? Models should be able to load libraries... I'll test that again now, but more data will help =)

-Edit- Working fine on all php versions I have
#6

[eluser]gtech[/eluser]
yes I have tested loading a library in a model and it works, not a good example of echoing content in a model! but just to show it works.

controller (application/controllers/temp.php):
Code:
<?php
class Temp extends Controller {

  function Temp()
  {
    parent::Controller();
  }
  function index()
  {
    $this->load->model('tester');
    $this->tester->loadlib();
  }
}
?>
library (application/libraries/template.php):
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class Template {
    function dotemplate()
    {
      echo "library";
    }
}
?>
model (application/models/tester.php):
Code:
<?php

class Tester extends Model
{
  function Tester()
  {
    parent::Model();
  }

  function loadlib()
  {
    $this->load->library('template');
    $this->template->dotemplate();
  }
}
?>

to add to what the others have said have you actually loaded the library in the controller to see if it works?
#7

[eluser]Seppo[/eluser]
Oh oh I got it! =)
Are you autoloading the model and loading the library in your constructor? If that's so that's the problem... my code example

The autoload config
Code:
$autoload['model'] = array('testmodel');

The testmodel model
Code:
<?php
class Testmodel extends Model {
    function Testmodel()
    {
        parent::Model();
        $this->do_test();
    }

    function do_test()
    {
        $this->load->library('validation');
        $this->validation->set_fields(array('a' => 'a'));
    }
}
?>
#8

[eluser]crumpet[/eluser]
hmm i feel really stupid but it didn't seem to work .. i've rewritten the code since then to not use libraries in the model but I tested it pretty throughly... i wasn't autoloading any of hte libraries/models so i don't think thats a problem. And though originally I was loading the library in the constructor of the model I tried loading it in the same function as the library function is called. And yes the library works perfectly in a controller and i copy pasted the code for loading the library there into the model.

So I just tested it using bare bones functions and that seemed to work so it must have been an error in my code elsewhere...really strange though because i'm sure i tested it this way before
thanks everyone for your help I will post again if I figure out what the problem was exactly.
#9

[eluser]gtech[/eluser]
was a good call by Seppo, maybe you were getting some strange php error, worth upping the log_threshold number it the config file to see if anything gets logged next time.
#10

[eluser]David B.[/eluser]
Hello,

I think I have kind of a similar problem. I'm not autoloading the model but I'm loading the the library in the constructor of my model. And I can not access the methods of the library in the constructor - only afterwards.

Please, can someone describe what is the problem with this?
And would the preferred solution for that be an additional init-method in the model that then should be called in the controller after loading the model?

E.g.:

Controller:
Code:
class Mycontroller extends Controller
{
    function someFunction()
    {
        $this->load->model("mymodel");
        $this->mymodel->init();
    }
}

Model:
Code:
class Mymodel extends Model
{
    function __construct()
    {
        parent::Model();
        $this->load->library("MyLib");
        
        // doesn't seem to work...
        //$this->mylib->setSomething("some value");
    }

    public function init()
    {
        $this->mylib->setSomething("some value");
    }
}

Thanks for your thoughts!

David




Theme © iAndrew 2016 - Forum software by © MyBB