When I want to access a function of a user defined model in CodeIgniter inside a custom user defined library it throws
Call to a member function Set_where() on a non-object
Although I load the model by using this inside that library
$CI =& get_instance();
$CI->load->model('home_model');
And I'm using this code to access the function inside home_model class
$CI->home_model->Set_where("film_feature='Y'");
So now it throws the above error.
So could someone please help me solve this error?
You could try to instantiate your model object like:
include_once('path/modelname.php');
$home_model = new Home_model();
$home_model->set_where(....);
make sure your model extends de default CI model class.
You could also use $CI->load->model(modelname); instead of include... It's easier to deal with path problems, however it would be a little less efficient because load->model will instantiate an object.
Now i fix the issue just adding $CI =& get_instance(); into that function where i want to fetch previously i declare that $CI =& get_instance(); in another function of that class for which it was not instantiated.
My working code is now..
$CI =& get_instance();
$CI->load->model('home_model');
$CI->home_model->Set_where("film_feature='Y'");
Related
I have 2 different model files with one common function.
So I written that function in another model file trying to access that by using:
$this->load->model('model_1');
$this->model_1->com_fun();
But it is not working. How can I call the functions of one model class inside another model class?
Sometimes is useful to have in your controllers something like:
public function __construct(){
$this->CI =& get_instance();
}
So, when you need another model you can do:
$this->CI->load->model('another_codeigniter_model_name', 'another');
$something = $this->CI->another->com_fun();
class Mylib
{
function show_lib()
{
$obj=& get_instance();
$obj->load->module(‘login_check’);
$var=$obj->login_check->get_all_table_data();
print_r($var);
}
}
ERROR:-
Fatal error: Call to undefined method CI_Loader::module()
I Hope This Will Work --> check this code
class Mylib
{
function show_lib()
{
protected $ci;
$this->ci = &get_instance();
$this->ci->load->library(‘login_check’);
$var=ci->load->login_check->get_all_table_data();
return $var;
}
}
Not module, it's a library
use:
$this->load->library();
Working with modules in CI is called HMVC - Hierarchical Model View Controller.
There is a beautiful modular extension for CI to work with - Modular Extensions - HMVC
By using this extension you can create and work with modules in CI.
After setting up HMVC Extension you can call modules from your controller.
$controller = $this->load->module('module_name/controller_name');
echo $controller->method();
Few tutorials to get started:
http://net.tutsplus.com/tutorials/php/hvmc-an-introduction-and-application/
http://www.extradrm.com/blog/?p=744
If you are using codeigniter 3 you need to use Codeigniter Object to load modules,helpers..etc. Assign Codeigniter object to a variable and use it. $CI =& get_instance() Sample code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CustomLibrary{
public $random_number;
public function __construct(){
$CI =& get_instance();
$CI->load->helper('string');
}
}
For more information about this visit http://www.webnsyntax.com/
When I try to call
$this->load->database();
yields the following error `"Call to a member function database() on a non-object"
Autoloading the database doesnt help too...
when I try to autoload it.
all calls to database like
$this->db->get('people');
it says get method is undefined...
I have no clue what and where to start..\
anyone ?
Go to autoload.php in application/config/autoload.php and add this
$autoload['libraries'] = array('database'); // add database in array
Make sure your connection settings are fine in application/config/database.php
Than in the library do it like this
Class MyLib
{
function getPeople(){
$CI = &get_instance();
$query = $CI->db->get('people');
return $query->result();
}
}
Use extends CI_Model if not working try extends Model
class User_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
}
You can load the database by two methods:
Method 1:Automatic Connecting
$autoload['libraries']=array('database');
Method 2: Manual Connecting
$this->load>database();
i hope above methods clear your confusion....
You are doing a very common mistake. When you call $this->load->database(); from controller or model it works because controllers and models are child of CI_Controller and CI_Model respectively. But when you are call them from Library which is not a child class of any basic CIclass you cannot load database() or anything else using $this-> key. you must use the help of &get_instance(); to load codeigniter instance and use that instance instead of $this. Which suggests following Code:
$INST=&get_instance();//Store instance in a variable.
$INST->load->database();//If autoload not used.
$INST->db->get('people');//or Your desired database operation.
It is better to keep a field variable to hold the reference to $INSTas you may need to access it in various functions.
Following Code will be more eligent:
class MyLib{
var $INST;
public function __construct()
{
$INST=&get_instance();//Store instance in a variable.
$INST->load->database();//If autoload not used.
}
function getPeople(){
$query = $INST->db->get('people');
return $query->result();
}
}
I have installed Wanwizard's Datamapper (http://datamapper.wanwizard.eu) in an empty codeigniter setup.
I have no problem using datamapper in my controllers, but when I try to call datamapper objects in my libraries.
I get the error: Fatal error: Class 'User' not found.
This is my code for my library:
class Auth {
protected $ci;
public function __construct()
{
$this->ci =& get_instance();
$this->ci->load->library('datamapper');
}
public function signup( $username, $email, $password )
{
$user = new User();
}
}
Does anyone know the correct way to call these datamapper objects within libraries?
You probably have resolved this already. To folks of the future visiting this:
The issue, as WanWizard points out, is either:
You aren't autoloading datamapper in application/config/autoload.php:
$autoload['libraries'] = array('database', 'datamapper');
The application/models folder does not contain a User.php file.
The User.php file does not contain a class User extends Datamapper{}.
Also -- Linux is a case sensitive operating system, be careful to follow the cases used in the Datamapper and CodeIgniter documentation when naming your files.
I have a custom helper that i use for logging.
Within one of the functions of the helper i need to get the name of the controller that was called. Is there a way to do it?
I can't rely on uri segments because some controllers are in sub-folders and the helper is used all over.
You can use the following in CI2.x
$this->router->fetch_class();
You may need to get an instance of the CI super variable $this first- in which case. Use the following:
$ci =& get_instance();
$ci->router->fetch_class();
There's also a $ci->router->fetch_method(); method if you need the name of the method called for any reason.
$this->>router->fetch_method(); will return index if you do something like this:
class Someclass extends CI_Controller {
function index(){
$this->edit();
}
function edit(){
$this->router->fetch_method(); //outputs index
}
}
this should work (not so sure if it works in the helper):
$ci =& get_instance();
$ci->router->class // gets class name (controller)
$ci->router->method // gets function name (controller function)
You can also use the URI class
$ci = & get_instance();
$ci->uri->segment(1) // That stands for controller
$ci->uri->segment(2) // That stands for method