Codeigniter : Cant load database in my library. - codeigniter

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();
}
}

Related

CI_Template' not found

I want to load model using
$this->load->model('apotek_data');
function __construct()
{
parent::__construct();
$this->load->model('apotek_data');
$this->template->write_view('sidenavs', 'template/default_sidenavs', true);
$this->template->write_view('navs', 'template/default_topnavs.php', true);
$this->load->database();
}
But when I put it ($this->load->model) in my code, the view doesn't show, and display an error message.
Fatal error: Class 'CI_Template' not found in
D:\wamp64\www\apotek\system\core\Common.php on line 196
What should I do?
Load the template library:
$this->load->library('template');
OR:
In config/autoload.php add template in the libraries array.
I don't know the name of your library so I am assuming it is template if it is not - change it with the name of the library.
For any other person experiencing a 'CI_Template not found' error:
In my case, I was loading a library (in the constructor) with the same name as the controller that was being accessed.
class Authld extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
I had to change the name of the controller for the issue to be resolved.
class Authentication extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
Your Template.php maybe looks like this:
class Template extends MX_Controller{
If so, change the MX to CI (CI_Controller).

How do I load a model method into a controller constructor in CodeIgniter?

I am trying to extend the CI_Controller class to load my global page header file so I don't have to load it at the beginning of every single controller method. It doesn't seem to be working. I know the Controller extension itself works... if I remove the call of the model method from the constructor and load it from my controller method, the rest of the controller extension works fine. But when I load the model method from within the constructor of the controller extension, I get a blank page(I haven't generated the main content yet).
Any ideas?
application/core/MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
var $user = array();
function __construct(){
parent::__construct();
$this->load->model('member');
if($this->session->userdata('member_id')){
$this->member->get_info($this->session->userdata('member_id'));
$this->user = $this->member->info;
$this->member->update_activity($this->session->userdata('member_id'));
} else {
$this->load->helper('cookie');
if(get_cookie('Teacher Tools Member Cookie')){
$this->member->auto_login(get_cookie('Teacher Tools Member Cookie'));
} else {
$this->user = $this->member->default_info();
}
}
$this->load->model('template');
$this->template->overall_header();
}
}
application/models/template.php
<?php
class Template extends MY_Model {
function __construct(){
parent::__construct();
}
function overall_header($title = 'Home'){
$data = array(
'BASE_URL' => base_url(),
'MAIN_NAVIGATION' => $this->main_navigation(),
'TOOLBAR' => $this->toolbar()
);
return $this->parser->parse('overall_header.tpl', $data);
}
MY_Model is an extension of the CI_Model class to load member information into $this->user.
I think response generation is done in Controller's method and all HTML pieces that you might have gets glued there. So if you are calling /controller/method_a then method_a will be responsible for returning response whereas in constructor you cannot set response.
I agree with you on setting important data in Constructor once so that you don't have to do this again and again in each method. I think you should assign output to some Controller level variable and then use that variable in your Controller's method.
I' am sure you got my point.
For this reason there is a config/autoload.php:
$autoload['model'] = array('YourModel');

Codeigniter model can not connect to db

My controller can connect to the DB but my model can not. I have autoloaded the DB in the autoload.php file, but no luck in the model.
so if I do something like
$this->db->insert('table', $data);
I receive this Call to a member function insert() on a non-object I have used Codeigniter before but have never had this issue, on my other project I did not even use parent::__construct()
class Bucketlist extends CI_Model {
private $data = array();
public function __construct(){
parent::__construct();
}
// Setter Function
public function __set ($var, $val) {
$this->data[$var] = $val;
}
// Getter Function
public function __get($var) {
return (isset($this->data[$var])) ? $this->data[$var] : null;
}
// Create WishList
function createBucketList($bucketlist) {
$this->db->insert('_bucketlist', $bucketlist->data);
}
}
thanks.
You may need to Autoload the Database connection (http://ellislab.com/codeigniter/user-guide/database/connecting.html), as it appears that the db variable is not instantiated before you are trying to use it.

Autoload libraries in Codeigniter

I am a newbie to Codeigniter.
I have 3 libraries in autoload in config.php .
But in one of my controllers I don't want to load the libraries. Is this possible?
If you need any library throughout the application you can load it in the config file and it will be auto loaded. But if you need a library only in a specific controller you can load it in the controller where you need it.
Class test Extends CI_Controller{
function index()
{
$this->load->library('mylibrary');
$this->mylibrary->somemethod();
}
}
Or if you need library through out the controller you can load it in the constructor.
Class test Extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->library('mylibrary');
}
function index(){
$this->mylibrary->somemethod();
}
function test(){
$this->mylibrary->someothermethod();
}
}
Extend CI_Controller in your libraries.
Something like this:
class MyLibrary extends CI_Controller {
var $ci;
function __construct() {
$this->ci = &get_instance();
$route = $this->ci->router->fetch_class();
if( $route == strtolower('YourController') ) return false;
}
}
you can remove libraries from autoload file. then they will not be activ in the framwork.
If you want to use them, you can call them in constructors if you want them in a class. If you want to use them in just method, you load them in the method.

CI Bonfire: Cannot call methods on Model

I am new to BF. I am learning BF by following the tutorial here, but I cant figure out why it keep prompt me error message as below:
Fatal error: Call to a member function where() on a non-object in ...
Here is the code:
class Content extends Admin_Controller {
public function __construct(){
parent::__construct();
Template::set('toolbar_title', 'Manage Your Blog');
}
public function index(){
$posts = $this->post_model->where('deleted', 0)->find_all();
Template::set('posts', $posts);
Template::render();
}
}
Can someone guide me on this? Thanks
bborisovs is right, you need to load the model in the constructor, before you can use the obgject:
$this->load->model('post_model', null, true);
Also make sure your model exists.

Resources