Call to a member function insert() on a non-object, codeigniter - codeigniter

I am trying to insert data to mysql in codeigniter. Controller class :
class Ci_insert extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$data = array(
"USN" => "TRE5rCS89G",
"name" => "NITISH DOLAKASHARIA",
"branch" => "CS"
);
$this->load->model('ci_insert_model');
$this->ci_insert_model->addToDb($data);
}
}
Model Class :
class ci_insert_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function addToDb($data)
{
//var_dump($data);
$this->db->insert('class_record',$data);
}
}
But when I tried to run the code, it shows Fatal error: Call to a member function insert() on a non-object in C:\wamp\www\CodeIgniter\application\models\ci_insert_model.php on line 12.
Whats wrong with the code above?

You're missing $this->load->database();
$this->db->method_name(); will only work when the database library is loaded.
If you plan on using the database throughout your application, I would suggest adding it to your autoload.php in /application/config/.
As others have mentioned, remove the CI_ prefix from your class names. CI_ is reserved for framework classes.

You have to use '$this->load->library('database')' in the model before '$this->db->insert()' or
autoload the database library. Go to config folder select autoload.php search for $autoload['libraries'] and replace your empty array() with array('database').

Add autoload libraries in config folder open autoload.php and set $autoload['libraries'] = array('database');

Try out.
Controller : insert.php
class Insert extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$data = array(
"USN" => "TRE5rCS89G",
"name" => "NITISH DOLAKASHARIA",
"branch" => "CS"
);
$this->load->model('insert_model');
$this->insert_model->addToDb($data);
}
}
Model: insert_model.php
class Insert_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function addToDb($data)
{
//var_dump($data);
$this->db->insert('class_record',$data);
}
}
Please write capital first latter of class, don't add prefix like ci_.

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).

error in codeigniter ( data not inserted in mysql database)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Employee extends CI_Controller {
public function _construct()
{
parent::_construct();
$this->load->model('employee_model');
$this->load->helper(array('form','url'));
}
public function index()
{
$this->load->view('employee_form');
}
public function employee_form()
{
$save=array(
'emp_name' => $this->input->post('emp_name'),
'emp_gender' => $this->input->post('emp_gender'),
'emp_email' => $this->input->post('emp_email'),
'emp_phone' => $this->input->post('emp_phone'),
'emp_address' => $this->input->post('emp_address')
);
$this->employee_model->saveemployee($save);
redirect('employee/index');
}
}
This is my code above and error shown blow I am fresher in CodeIgniter I need help
This is an error
Alway model name is start from capital letter. First fixd that. Then chek database setting
You have to load the db library first. In
autoload.php :
$autoload[‘libraries’] = array(‘database’);
public function _construct()
{
parent::_construct();
$this->load->model('employee_model');
$this->load->helper(array('form','url'));
private $employee_model = 'employee_model';
}
First set it in construct
and then, call it like this
$this->{employee_model}->saveemployee($save);

Autoload controller in CodeIgniter

I have question to you.
I try add to my page calendar and some events in this calendar. I know how I can load calendar in page, but I didn`t now how I can load this calendar on every page automatically.
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar extends CI_Controller {
function index()
{
$data = array(
3 => 'Polska - Anglia',
);
$this->load->library('calendar');
$vars['calendar'] = $this->calendar->generate('', '', $data);
$this->load->view('main/calendar', $vars);
}
}
and In view I call:
<?php echo $calendar;?>
I use CodeIgniter 2.1.3
Instead of creating controller for calendar, create a library class and then add it to autoload configuration file
class MyCalendar{
public function get()
{
$CI =& get_instance();
$data = array(
3 => 'Polska - Anglia',
);
$CI->load->library('calendar');
return $CI->calendar->generate('', '', $data);
}
}
Add this library to autoload file and then you can call it anywhere you want by using this statement.
$data['calendar'] = $this->MyCalendar->get();
You can autoload your library by changing the application/config/autoload.php file.
Find :
$autoload['libraries'] = array();
Replace by :
$autoload['libraries'] = array('calendar');
To load the same calendar on all your pages, I suggest to build a parent controller in the application/core folder.
abstract class BaseController extends CI_Controller
{
protected $data;
public function __construct()
{
parent::__construct();
$this->data = array();
$calendarData = array(
3 => 'Polska - Anglia'
);
$this->data['calendar'] = $this->calendar->generate('', '', $calendarData);
}
}
You can then extend that BaseController class on all your controllers.
class Calendar extends BaseController {
function index()
{
$this->load->view('main/calendar', $this->data);
}
}
Be sure to always use $this->data to build on the protected member of your class.
Lastly, your BaseController will not be autoloaded, you probably don't want to include it everywhere. I suggest you to add the following method at the end of your autoload or config file.
/**
* Extra autoload function. Loads core classes automatically.
* #param type $class The class to load.
*/
function __autoload($class)
{
if (strpos($class, 'CI_') !== 0)
{
if (file_exists($file = APPPATH . 'core/' . $class . EXT))
{
include $file;
}
}
}
This will allow you to autoload any classes you have in your application/core folder. This solution might seems complex, but once it's setup. You can add functionality to your BaseController that is applicable for all pages, for example, header and footer stuff.
Hope this helps!

HMVC Module Error - Undefined property: model

So I'm getting the error: Undefined property: badge_progress::$bp_model.
I don't understand what's going on. Here is my code:
Controller:
<?php
// Badge Progress Module
class badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
?>
Model:
<?php
class bp_model extends CI_Model {
function dotpoints($badge_id) {
$query = $this->db->query("SELECT * FROM course_topic_dotpoints WHERE badge_id = ".$badge_id);
if ($query->num_rows() > 0) {
return $query->result();
}
}
}
?>
Ah fixed it! Didn't realise that the main controllers (controllers outside of the module directory) also needed to be extending "MX_Controller" instead of "CI_Controller".
Class names must begin with an uppercase letter.
class Badge_progress extends...
class Bp_model extends...
http://codeigniter.com/user_guide/general/controllers.html
http://codeigniter.com/user_guide/general/models.html
update:
You shouldn't have the logic you need as a function in your constructor. Create a separate function to process the dotpoints stuff.
<?php
// Badge Progress Module
class Badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
}
function dotpoints()
{
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
Also, you are missing the constructor in your model. Check out those links I posted earlier...

How to Inherit A Model from Another Model in CodeIgniter

i'm using codeigniter for my project and i have this class model which i call Genesis which looks like this:
class Genesis_model extends CI_Model {
function __construct() {
parent::__construct();
}
function get() {
return 'human soul';
}
}
and i have another model, stored in the same directory, which extends Genesis_model
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return $this->get();
}
}
Human_model is used by Human controller
class Human extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('human_model');
}
function get_human() {
$data['human'] = $this->human_model->get_human();
$this->load->view('human/human_interface', $data);
}
}
if i execute the code, it will produce an error which point to return $this->get(). it reads "Fatal error: Class 'Genesis_model' not found in ...\application\models\human_model.php on line 2".
i use this method because nearly all my models shared almost identical structure. I gather the similar functionality in Genesis while the other models serve only as data suppliers unique to the tables they represent. it works well in my asp.net (vb.net) but i don't how to do it in codeigniter.
is there a way for Human_model to inherit Genesis_model. i don't think i'm allowed to use include('genesis_model.php'). i don't know if it works either.
thanks in advance.
core/MY_Model is good if there's only 1 important superclass for your models.
If you want to inherit from more than model superclass, a better option is to change your autoload configuration.
In application/config/autoload.php, add this line:
$autoload['model'] = array('genesis_model');
Put the file genesis_model.php in the core directory
Change your Human_model to this:
include('genesis_model.php');
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return parent::get();
}
}
notice the get_human function and the include.
You have to include the Genesis_model on your Human_model.php like this:
include_once( APPPATH . 'folder/file' . EXT );
Or you can autoload it on your config/autoload.php file, what I think is stupid =)
other solution
<?php
$obj = &get_instance();
$obj->load->model('parentModel');
class childModel extends parentModel{
public function __construct(){
parent::__construct();
}
public function get(){
return 'child';
}
}
?>

Resources