I want to access my database models from database.php file.
`
//$ci =& get_instance();
$this->CI->session->userdata('user_id');
$this->CI->load->model('users/user_model');
$current_user = $this->CI->user_model->find($this->CI->auth->user_id());
echo $current_user->organisation;
$this->CI->load->model('organisation/organisation_model');
$org = $this->CI->organisation_model->find($current_user->organisation);
`
Uncomment your first line
$ci =& get_instance();
Then use $ci instead of $this like this
$ci->session->userdata('user_id');
Please use the below code, to access the database model or queries.
$CI = & get_instance();
$CI->session->userdata('user_id');
$CI->load->model('users/user_model');
$current_user = $CI->user_model->find($CI->auth->user_id());
echo $current_user->organisation;
$CI->load->model('organisation/organisation_model');
$org = $CI->organisation_model->find($current_user->organisation);
Related
I have a query to you all, I want to make below function globally in codeigniter:
$ci =& get_instance();
I have an fatal error:
Fatal error: Uncaught Error: Using $this when not in object context:
I want to use $ci for $this but how can I mention $ci =& get_instance() globally?
I have to put $ci =& get_instance(); code in every function and every file.
please help.
Here is my test code:
public static function index()
{
$ci =& get_instance();
$redirect = $ci->auth->is_logged_in(false, false);
}
Solved:
The issue is php version:
old version
$this->employees_model->get_all();
new version
$ci =& get_instance();
$ci->employees_model->get_all();
You can create helper... In that you are able to create function with $ci =& get_instance(); & access it directly in controller,model,view etc.
example :
if (!function_exists('isLogin'))
{
function isLogin(){
//get main CodeIgniter object
$ci =& get_instance();
//your code
}
}
access directly isLogin() in controller like:
if(isLogin())
{
//Your code
}
Fatal error: Call to a member function base_url() on a non-object
I am getting the above error when I redirect to the site from payment gateway, hence the head file containing all the scripts does not get loaded
I tried using $ci =& get_instance(); But still its giving the same issue
public function payment_success() {
$this->load->model("bidding_m");
$this->load->model("admin_setting_m");
$this->load->model("channel_partners_m"); //call model functions to update the tables
$this->load->config('payu_config', TRUE);
$this->config = $this->config->item('payu_config');
//echo base_url();
exit;
}
Your problem is this line:
$this->config = $this->config->item('payu_config');
I don't know if this code is in library / controller so I tried explain it for both:
Controller:
You rewrite whole config ($this->config = ...) with a single config item and your base_url config item is lost! So do it this way:
$config = $this->config->item('payu_config');
Library:
You are accessing config item, but don't have instance of codeigniter. If this code is in library, it should looks like this:
public function payment_success() {
$ci = & get_instance();
$ci->load->model("bidding_m");
$ci->load->model("admin_setting_m");
$ci->load->model("channel_partners_m");
$ci->load->config('payu_config', TRUE);
//if you have in this library variable $config
$this->config = $ci->config->item('payu_config');
echo base_url();
exit;
}
You could try loading the helper like so
Filename first letterupper case
Somelibraryname.php
<?php
class Somelibraryname {
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->helper('url');
}
public function something() {
echo base_url();
// Or
echo $this->CI->config->item('base_url');
}
}
how I can replace $this variable in this code? I know that it is incorrect to use $this in functions, but I don't know how I may replace that.
function get_dates($id) {
$class = $this->db->query("SELECT * FROM schedule WHERE class_id='".$id."'");
$class = $class->result_array();
return $class;
}
PS. I don't won't to work with mysql with standart php features and I strongly believe that it is possible to solve this problem with CI featues.
If you want to access the CI framework from inside a plain function, you need to get an instance of CI.
function get_dates($id) {
$ci =& get_instance();
$class = $ci->db->query("SELECT * FROM schedule WHERE class_id='".$id."'");
$class = $class->result_array();
return $class;
}
If You are in Helpers or library and want to access the controller object you have to user:
$ci =& get_instance();
Once you will get the controller object you can access any public method or member through $ci reference, you can get the model object as well by setting up a getter method to get the object and then simply reference that object for querying the db.
I want to write a function for loading dropdown in helper file and for that reason I want to Use my models in Helper file.
When I use this it give me the error:
$this->load->model("news_model");
The Error:
Fatal error: Using $this when not in object context in C:\xampp\test\application\helpers\component_helper.php on line 6
my method:
function dropdown($Class,$Attribute)
{
$Output=NULL;
$ClassName=$Class."_model";
$this->load->model($ClassName);
$FullData=$ClassName->get();
foreach ($FullData as $Data)
{
$Output.='<option value="'.$Data->Id.'">'.$Data->$Attribute.'</option>';
}
return $Output;
}
Thanks
Check this post:
function my_helper()
{
// Get a reference to the controller object
//$CI = get_instance();
// use this below
$CI = &get_instance();
// You may need to load the model if it hasn't been pre-loaded
$CI->load->model('my_model');
// Call a function of the model
$CI->my_model->do_something();
}
https://stackoverflow.com/a/2479485/1570901
Helper functions are functions. This means they are not bound to an object nor class. $this is therefor not avaible!
You have to get the instance of CodeIgniter-Core from somewhere else!
CodeIgniter provides a get_instance(); function for that:
$CI = get_instance();
Now wereever you had $this->load etc.
Replace $this with $CI to call on the CodeIgniter Core!
And by the way you are calling the model wrong. You have to access it via the CI-core:
$CI->$Classname->get();
Here is the correction for you code:
function dropdown($Class,$Attribute)
{
$CI = get_instance();
$Output=NULL;
$ClassName=$Class."_model";
$CI->load->model($ClassName);
$FullData=$ClassName->get();
foreach ($FullData as $Data)
{
$Output.='<option value="'.$Data->Id.'">'.$Data->$Attribute.'</option>';
}
return $Output;
}
Now instead of using $this, always use $CI inside this method.
Hope this helps.
One of my CodeIgniter Controller functions needs to call a recursive function as part of its functionality. The function call chokes if I put it inside the controller class, and it can't access database functions ($this->db->get()) if I put it outside the class. Would making it a helper function fix this problem?
You can get instance:
$CI =& get_instance();
After that you will be able to use $CI->db for queries..
If you want to use $this in libraries, helpers, and access all the methods:
$this->ci =& get_instance();
$this->ci->load->database();
You can do also:
$this->ci->config->item('languages');
or
$this->ci->load->library('session');
We can define a function in helper
if (!function_exists('getRecordOnId'))
{
function getRecordOnId($table, $where){
$CI =& get_instance();
$CI->db->from($table);
$CI->db->where($where);
$query = $CI->db->get();
return $query->row();
}
}
and we can call from view like
$recordUser = getRecordOnId('users', ['id' => 5]); //here 5 is user Id which we can get from session or URL.
//Select Data:
$this->db->select(‘fieldname seperated by commas’);
$this->db->from(‘table’);
$query = $this->db->get();
$results=$query->result() ;
//Joins:
$this->db->select(‘*’);
$this->db->from(‘table1′);
$this->db->join(‘table2′, ‘table2.id = table1.id’);
$query = $this->db->get();
We may get it from
http://skillrow.com/codeignitor-database-functions/