Codeigniter model can not connect to db - codeigniter-2

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.

Related

error for inserting data in database in view in joomla

I want to insert a data in database but it show error
Fatal error: Call to a member function getform() on a non-object in
\components\com_enquiry\views\form\view.html.php
on line 9
my code is:
enqiry.php:
public function getform($data)
{
$db=JFactory::getDbo();
$db=$this->getDbo();
$query=$db->getQuery(true);
$reg=new stdClass();
$reg->name=$data['name'];
$reg->email=$data['email'];
$reg->phone=$data['phone'];
$reg->comments=$data['comments'];
$reg=$db->insertObject('#__enquiry',$reg);
}
and view.html.php:
public function display()
{
$this->msg = 'enquiry form';
$model=$this->getModel();
$data =$model->getform();
$this->assignRef('data', $data );
parent::display();
parent::display();
}
controller:
class enquiryController extends JControllerLegacy
{
public function display()
{
$vname=JRequest::getCmd('view','form');
JRequest::setVar('view',$vname);
JRequest::setVar('layout','default');
parent::display();
}
public function show()
{
$data['name']=$_POST['name'];
$data['email']=$_POST['email'];
$data['phone']=$_POST['phone'];
$data['comments']=$_POST['comments'];
$sname=JRequest::getCmd('view','thanx');
JRequest::setVar('view',$sname);
print_r($data);
}
}
could someone help me solve this error?
The problem is that your model is not loaded by the line:
$model=$this->getModel();
This either means that your model in enquiry.php is not named correctly
ComponentNameModelEnquiry
or that your view is not named enquiry.
You can try to specify which model you want, like
$model = $this->getModel('enquiry');

Codeigniter : Cant load database in my library.

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

CodeIgniter routing issue, advice how to do it

I'm not CI programmer, just trying to learn it. Maybe this is wrong approach, please advice.
my controller(not in sub directory) :
class Users extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index($msg = NULL) {
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function process_logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}
And a route for login :
$route['user/login'] = 'users/index';
Problem is when I wanna logout, it shows me 404 because I do not have it in my route :
$route['user/process_logout'] = 'users/process_logout';
and in my view I put logout
When I add that, it works, and that is stuppid to add a route for everything. What I'm I doing wrong, please advice.
Thank you
Don't know why you are trying to implement login feature in index() function. However since you said you are learning CI I'm telling something about _remap() function.
Before that. You can try the following routing:
$route['user/:any'] = 'users/$1';
$route['user/login'] = 'users/index';
If you want to take value immediately after controller segment you need to use _remap() function and this function may be solve your routing problem, i mean you don't need to set routing. Lets implement your code controller 'users' using _remap() function.
class Users extends CI_Controller {
private $sections = array('login', 'logout');
function __construct() {
parent::__construct();
}
public function _remap($method)
{
$section = $this->uri->segment(2);
if(in_array($section, $this->sections))
call_user_func_array(array($this, '_'.$section), array());
else show_404(); // Showing 404 error
}
private function _login()
{
$msg = $this->uri->segment(3);
$this->load->helper(array('form'));
$data['msg'] = $msg;
$this->load->view('user/login' , $data);
}
public function _logout() {
$this->session->sess_destroy();
redirect(base_url());
}
}

Assigning object data to a view in Codeigniter gives weird behaviour

I've got a problem when assigning data to an object variable. In all my time, i haven't seen such weird behaviour.
I have a class:
class Main extends CI_Controller {
public $data = array();
public function __construct(){
parent::__construct();
/**
* Load Models
*/
$this->load->model('rss');
$this->load->model('model_product');
/**
* Default loaded Data
*/
$this->data['newsfeed'] = $this->rss->load($this->config->item('newsfeed'));
}
}
You see that i want to add the newsfeed to the $this->data object. It will held in a key named newsfeed. But now, i want this newsfeed on different pages. It is useless and inefficient to load this on every page, so i extended my main controller.
class Shop extends Main {
public function __construct(){
parent::__construct();
}
public function index()
{
$this->data['content'] = 'shop/default';
$this->load->view('template/index', $this->data);
}
public function product($id){
$this->data['product'] = $this->model_product->getProducts(null, $id);
if($this->data['product']->categoryid != $this->config->item('gamecategory')){
redirect('');
}
var_dump($this->data['product']);
$this->data['content'] = 'shop/product';
$this->load->view('template/index', $this->data);
}
}
The expected behaviour would be that $this->data would hold an array of objects (in each key ) but when i var_dump in the shop controller it gives me the entire $data object as return data, instead of the provided key. This totally blows my mind and i cant seem to understand this weird behaviour.
Expected var_dump ($this->data['product']):
Array(x){
key: value,
key: value,
.....
}
actual result:
Object{
newsfeed:
- data
product:
- data
....
....
}
Model rss:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Rss extends CI_Model{
public function __construct(){
parent::__construct();
}
public function load($item){
return ($xml = simplexml_load_string(file_get_contents($item))) ? $xml : false;
}
}

Convenient Way to Load Multiple Databases in Code Igniter

I've added the appropriate configuration arrays to database.php and they work, however, I would like an easier way to access the different databases. Right now I have to do something like this in every controller method:
function index(){
$BILLING = $this->load->database('billing', TRUE);
$INVENTORY = $this->load->database('inventory', TRUE);
$data['billing'] = $BILLING->get('stuff');
$data['inventory'] = $INVENTORY->get('stuff');
}
I'd like to be able to put those first two lines in some sort of before filter or pre_controller hook.
You could simply load the database instances globally in your constructor, then they would be available to all controller methods...
example controller
class Example extends CI_Controller {
//declare them globally in your controller
private $billing_db;
private $inventory_db;
function __construct() {
parent::__construct();
//Load them in the constructor
$this->billing_db = $this->load->database('billing', TRUE);
$this->inventory_db = $this->load->database('inventory', TRUE);
}
function index() {
//Then use them in any controller like this
$data['billing'] = $this->inventory_db->get('stuff');
$data['inventory'] = $this->billing_db->get('stuff');
}
}
And if these same databases are used across multiple controllers, you might consider extending the base controller to include these global variables and load them in the constructor of your base controller in MY_Controller.php
example MY_Controller.php
class DB_Controller extends CI_Controller {
//declare them globally in your controller
private $billing_db;
private $inventory_db;
function __construct() {
parent::__construct();
//Load them in the constructor
$this->billing_db = $this->load->database('billing', TRUE);
$this->inventory_db = $this->load->database('inventory', TRUE);
}
}
Then you'd use it like this...
class Example extends DB_Controller {
function __construct() {
parent::__construct();
}
function index() {
//Then use them in any controller like this
$data['billing'] = $this->inventory_db->get('stuff');
$data['inventory'] = $this->billing_db->get('stuff');
}
}

Resources