Welcome Guest, Not a member yet? Register   Sign In
Using Library in Model
#1

[eluser]dorjeduck[/eluser]
Good morning,

hope all are well.

I just started writing a new CI app where I have a model which need some additional lib like classes. To have everything nicely together I wanted to put the libraries in the application/library folder and load and use the lib in my model. But I can't figure out how to load a library from that foler in the model, in the docu it just mention how to load libs in the controller. But this is something the controller shouldn't be involved with, its a pure model layer lib etc. Is this something which shouldn't be put in the libraries folder but included with "require"? I could get it done somehow of course but I was wondering about what would be the best practice here.

Thanks a lot

martin
#2

[eluser]Ty Bex[/eluser]
EXAMPLE

Create FILE: application/libraries/My_library.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_library {
    
    function My_library ()
    {
        /*DO SOMETHING */
    }
    
    function test_library()
    {
        /*DO SOMETHING ELSE*/
    }
}
?>

Edit FILE: application/config/autoload.php
Code:
$autoload['libraries'] = array('my_library' );  // Other libraries may be loaded in the array


In Your model: application/models/my_model.php
Code:
<?php
class My_model extends Model{

    function My_model()
    {
        parent::Model();
    }

    function myfunction()
    {
        $this->My_library->test_library();
    }
}
?>

This is the basic Idea.
#3

[eluser]dorjeduck[/eluser]
Thanks a lot Ty Bex,

I didn't understand so far that the once loaded the lib is also available in the model, great.

So a following up question, autoload as far as I understand means the lib is loaded whenever a request occurs, right? So this wouldn't be wanted in my case so I tried and load the lib in the specific controller which works. Still the controller has to think now for the model even so this lib is some model only stuff which my controller shouldn't be involved with. I still would prefer if I could load the lib in the model.
Particular I have to pass some model specific parameters when initializing the class and it looks odd to have this in the controller.
This might sound a bit picky now but just want to get my code as clean and loosely coupled as possible.

Any further suggestions highly appreciated.

Thanks

martin
#4

[eluser]Ty Bex[/eluser]
I am not sure that I understand your question. Can you show me some of your code to see what you are trying to do.

I am not sure if this is what you are asking but:
- To Load a library in only when a function in a model is called make sure that it is not in the autoload.php
- load it from the Model function.
- pass it variables


Code:
<?php
class My_model extends Model{

    function My_model()
    {
        parent::Model();
    }

    function myfunction()
    {
        $params = array(
                       'value1' => 'Informational Text One',
                       'value2' => 'Informational Text Two'
                       );
        $this->load->library('My_library', $params);
    }
}
?>

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_library {
    
    function My_library ($params)
    {
        /*DO SOMETHING with $params*/
    }
    
    function test_library()
    {
        /*DO SOMETHING ELSE*/
    }
}
?>
#5

[eluser]Teks[/eluser]
If your model objects are always going to be using functions from your custom library, you can perhaps load the library in your '__construct()' function - the class constructor. If the library is only needed in a couple of the model's functions - which might only occasionally get called - then you can load the library just inside those functions. Either way, you can use the $this->load->library method as shown by Ty Bex.
#6

[eluser]dorjeduck[/eluser]
Thanks a lot both of you,

this was really a whole series of confusions arisen on my side, partly around when to use capital letters, partly due to general stupidity. But finally thanks to your posts I got it - hopefully.

So you saved my code from ridiculous unnecessary workarounds.

Once more big thanks and praises to the CI community.

Cheers

martin




Theme © iAndrew 2016 - Forum software by © MyBB