CodeIgniter Forums

Full Version: Loading custom libraries
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]skaterdav85[/eluser]
Can you load a custom library in a model? Based on the user guide for creating custom libraries, it says you can load a custom library in a controller, but it doesnt say you cant load it in a model.

El Forum

[eluser]ShoeLace1291[/eluser]
I've never tried to load a library within a model since all the libraries I use are usually autoloaded. But if those libraries work in my models, then I assume you can since custom libraries are loaded the same way as CI libraries.

El Forum

[eluser]skaterdav85[/eluser]
ok thanks. also related to that question:

Does loading a library using the CI syntax:
$this->load->library('library_name')
create a new object?

Is that CI's way of creating an object like in regular php syntax, ie:
$myObject = new Email_Class();
?

El Forum

[eluser]CroNiX[/eluser]
[quote author="skaterdav85" date="1286965878"]ok thanks. also related to that question:

Does loading a library using the CI syntax:
$this->load->library('library_name')
create a new object?

Is that CI's way of creating an object like in regular php syntax, ie:
$myObject = new Email_Class();
?[/quote]

Yes, except the library then extends the current CI instance, and it is then accessed via
$this->library_name

Is there a reason why you can't pass whatever data you are needing from your controller to the view instead of getting data in the view from a library? If you can't directly pass it from your controller for some reason, call the library in the controller and then pass the data to the view. You shouldn't be accessing models or libraries or anything else (except helpers) in the view.

El Forum

[eluser]skaterdav85[/eluser]
So I am having a problem with my foreach loop. I added a custom library called RemoteConnector. In my model, I am retreiving a list of addresses from a table in my database and looping over each address and geocoding that address with the tiny geocoder webservice and my RemoteConnector library, all of which is happening in my model. Just for testing, I am echoing out each address and its coordinates in my model, and for some reason, the coordinates are all the same for every address. I have a feeling this has something to do with using the same instance of my RemoteConnector object, because the coordinates being echoed out are all the same from the first address.

Here is some of my code. My database result set is stored in a property called 'address' which im looping over in this foreach loop to get the coordinates:

Code:
foreach($this->address as $showLocation){    
            //now geocode each showLocation
            $address = urlencode($showLocation);
            $url = array("url"=>"http://tinygeocoder.com/create-api.php?q=$address");
            $this->load->library('RemoteConnector', $url);
            $coord = $this->remoteconnector->__toString(); //get the string contents of the remote file
            echo $showLocation." | ".$coord."\n";
}

El Forum

[eluser]CroNiX[/eluser]
if you look at the docs for the loader, you will see that the 2nd parameter is for naming that instance of the library to something else.

$this->load->library('some_library', 'call_it_this');

To access some_library now, you do
$this->call_it_this->method.

This allows you to have multiple instances of the same object.
$this->load->library('some_library', 'call_it_this');
$this->load->library('some_library', 'call_it_that');

produces 2 separate instances of some_library which are now accessed like:
$this->call_it_this->method
$this->call_it_that->method

What you need to do is make a $url variable in your remoteconnector library, and create a setter method to set it.
Code:
private $url = '';

function set_url($url)
{
    $this->url = $url;
}
then in your
__to_string method add:
$url = $this->url;
and use that parameter to get it.

Then in your loop...
Code:
$this->load->library('RemoteConnector'); //no second parameter, only load library once (outside of loop)

foreach($this->address as $showLocation){    
    //now geocode each showLocation
    $address = urlencode($showLocation);
    $url = array("url"=>"http://tinygeocoder.com/create-api.php?q=$address");
    $this->remoteconnector->set_url($url);     //set the url in the library  
    $coord = $this->remoteconnector->__toString(); //get the string contents of the remote file
    echo $showLocation." | ".$coord."\n";
}
I hope that makes sense.

El Forum

[eluser]CroNiX[/eluser]
although personally I would add the urlencode and everything else to the __toSting method and just pass $showLocation to that.
Code:
foreach($this->address as $showLocation){
    $coord = $this->remoteconnector->__toString($showLocation); //get the string contents of the remote file
    echo $showLocation." | ".$coord."\n";
}
and in your library, in
Code:
function __toString($url) {
    $location = array("url"=>"http://tinygeocoder.com/create-api.php?q=" . urlencode($url);
    ....
    return $coordinate;
}
Then you dont even need the setter.