hi i am using codeigniter , in my controller constructor sometimes i use $this sometimes $this->ci
in two constructors i have use like this
public function __construct()
{
$this->ci =& get_instance();
$this->ci->load->library('form_validation');
$this->ci->load->library('catalog/CatalogManager');
}
function __construct()
{
parent::__construct ();
$this->ci = & get_instance ();
$this->load->library ( 'auth_lib' );
$this->load->library ( 'session' );
}
when passing data to view i use
$this->ci->data and $this->data in above two cases .
neither gives errors , but i am confused , what is the correct use.
please help...........
All controllers extend the main CI_Controller, so calling something like $this->load means accessing the parent method load() inside the parent class CI_Controller.
$this->ci works because with $this->ci = &get_instance() you're calling a reference to the main controller class...again. If you look in the bootstrap file (IIRC. Or the codeigniter.php file) there's the function get_instance(), which does nothing but return (by reference) the instance of the CI_Controller class.
So, basically, calling $this->ci->load and $this->load are the same exact thing, only that the first is unnecessary within a Controller/Model/View because the system is already doing that in the parent class (through the method load).
If you have a look at libraries, for ex., you'll see instead that using $this->ci->method() is necessary, because you need to have available all the methods of the CI_Controller, which is a kind of "super class" that drives the whole framework.
Have a look at the loader class and the CodeIgniter class to grasp how CI internally works.
Agree with the answer above, but actually, load is a variable, not a function. it is a object of the class CI_Loader, when you call $this->load->libray(), in fact it calls the library() function in CI_Loader.
$this is nothing. It just use to store the value. It just like a variable.
Related
I am using codeigniter 2.1.3
I am trying to load a model from the library. Initially my code in the construct in the library looks like this
$this->CI=& get_instance(); $this->CI->load->database('default')
Then in one of my library methods
when I tried the line below it doesnt work
$this->load->model('model_name')
but when I tried this
$this->CI->load->model('model_name','',TRUE)
it works, anyone can explain what the instance of CI refers to and the 2 extra parameters when loading the model? Thanks in advance.
A library is not necessarily a part of the way CodeIgniter works.
It could be a homemade library, to solve a task that you want done in your CI application.
This means that if you want to use any of CI's helpers, models or other libraries, you need to do this through the CI instance. This is achieved by doing this:
public function __construct()
{
$this->CI =& get_instance();
}
By assigning the instance to your librarys member named CI, all CI related helpers, models and libraries can be loaded through $this->CI. By trying to do it only with $this you are only referring to the current library - not the CI instance.
To load your model correctly, in your library, $this->CI->load->model('model_name'); is enough. The second parameter allows you to access your model through a different object name. The third parameter is not necessary for loading models, but allows you to autoload the database driver.
If you want to access your model through the same member:
$respone = $this->CI->model_name->method();
I have very simple code that you should use to load model in library
<?php
class Custom_lib
{
private $_CI;
public function __construct()
{
$this->_CI = & get_instance();
$this->_CI->load->model('Dynamic_Model','dm');
}
function currentSession()
{
$session = $this->_CI->dm->fetchSingleData('id,session','session',array('is_active'=>1));
return $session;
}
}
I hope this code will help you
You can tell the model loading function to auto-connect by passing TRUE (boolean) via the third parameter, and connectivity settings, as defined in your database config file will be used:
$this->load->model('Model_name', '', TRUE);
You can more about this at the end of the page of this below link.
http://ellislab.com/codeigniter/user-guide/general/models.html
I want to access database functions in extending libraries.
class My_Router extends CI_Router {
public function __construct()
{
parent::__construct();
}
function _set_routing()
{
.....
....
$query=$this->db->get('custome_routes');
$custom_routes=$query->result_array();
....
}
}
I tried below also
$CI =& get_instance();
$query=$CI->db->get('custome_routes');
$custom_routes=$query->result_array();
But it doesn't work.
Help me....
Thanks in advance,
Logan
Unfortunately you can't do that, because the loading order of the framework, and since the routing information is used to decide what controller class to load, and the return value of get_instance() is that controller instance.
You can try using CI's built in class by require ing the system/DB.php and use the DB() function to get an instance of the db class.
public function _set_routing(){
require_once BASEPATH.'database/DB.php';
$db = DB('default', true); // you can set the database.php config's group, or a full DSN string here
$custom_routes = $db->get('custome_routes')->result_array();
}
Or you could make a CI independent db connection in your subclass, for this one "get the full table" query this shouldn't be hard. You should be able to include the CI database config file and use it's values without problems (watch out for environments).
Just load database from instance ( $CI->load->database(); )
$CI =& get_instance();
$CI->load->database();
$query=$CI->db->get('custome_routes');
$custom_routes=$query->result_array();
EDIT: This just work for libraries and does not work for core classes.
I want to call a function in a library inside another library which is written by me. Is it possible to do this in codeigniter? If so, can anyone explain how to do that?
You could do;
$CI =& get_instance();
$CI->load->library('your_library');
$CI->your_library->do_something();
Typically, you reference the Codeigniter object (the current controller, technically) by using get_instance(). Often you'll want to assign it to a property of your library, like this:
class My_Library {
private $CI;
function __construct()
{
// Assign by reference with "&" so we don't create a copy
$this->CI = &get_instance();
}
function do()
{
$var = $this->CI->my_other_library->get();
// etc.
}
}
Just make sure the other library is loaded or in your config/autoload.php.
I am putting this code in the constructor for a model class, based on teh tutorial for CI, it states that if you put it there, the database connection can be used globally within that class afterwards. For some reason it's not working and the application crashes at that part of the code. My database configuration is fine since when i put it in the controller i'm able to get db info fine.
Are you doing that before or after the parent class's constructor?
public function __construct()
{
// placing it here fails: $this has no `load` property yet.
// $this->load->database(); <!-- NO WAY JOSÉ!
parent::__construct();
// placing it here should work as the parent class has added that property
// during it's own constructor
$this->load->database();
}
On the other hand, you could be even more explicit:
public function __construct()
{
// Doesn't matter where this goes:
// grab the controller directly
$CI =& get_instance(); // & is not strictly necessary, but still...
// force the loader to load the database.
$CI->load->database();
// directly assign it.
$this->db = $CI->db;
// continue on your merry way
parent::__construct();
}
I believe the explicit solution solved a number of problems in a PHP 4 project once, but it is technically overkill.
you dont need to initialize that . better configure it into
application - config - autoload.php file like this
$autoload['libraries'] = array('database');
The line of code to load the database object is:
$this->load->database();
The database object is then referenced with the name db like so:
$this->db->method_name();
As pointed out by the previous post, if you are going to use the database across multiple models, you should have the library autoloaded in your autoload.php config file.
I am new to codeigniter.
I want to know use of $CI =& get_instance();
is this use for error logging or global config variable.
thanks in advance.
From CodeIgniter manual:
$CI =& get_instance();
Once you've assigned the object to a
variable, you'll use that variable
instead of $this:
$CI =&get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url'); etc.
Note: You'll notice that the above
get_instance() function is being
passed by reference:
$CI =& get_instance();
This is very important. Assigning by
reference allows you to use the
original CodeIgniter object rather
than creating a copy of it.
Also, please note: If you are running
PHP 4 it's usually best to avoid
calling get_instance() from within
your class constructors. PHP 4 has
trouble referencing the CI super
object within application constructors
since objects do not exist until the
class is fully instantiated.
Link:
http://codeigniter.com/user_guide/general/creating_libraries.html
Only the class that extends CI_Controller,Model,View can use
$this->load->library('something');
$this->load->helper('something');//etc
Your Custom Class cannot use the above code.
To use the above features in your custom class, your must use
$CI=&get instance();
$CI->load->library('something');
$CI->load->helper('something');
for example,in your custom class
// this following code will not work
Class Car
{
$this->load->library('something');
$this->load->helper('something');
}
//this will work
Class Car
{
$CI=&get_instance();
$CI->load->library('something');
$CI->load->helper('something');
}
// Here $CI is a variable.