CI Bonfire: Cannot call methods on Model - codeigniter

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.

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

Codeigniter function undefined error

My custom core Class in application/core/MY_Controller.php
class MY_Controller extends CI_Controller{
protected $data = array();
function __construct(){
parent::__construct();
}
function rander_page($view){
//do this to don't repeate in every controller
$this->load->view('includes/header');
$this->load->view('top_menus');
$this->load->view($view, $this->data);
$this->load->view('includes/footer');
}
}
Index Controller in application/controllers
class Index extends MY_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->render_page('index');
}
}
Error is :Fatal error: Call to undefined method Index::render_page() in D:\wamp\www\ci\application\controllers\index.php on line 10
i am trying to use one controller for all pages help plz
Yes, the error is the clue itself, it has mentioned the function is undefined. You have not defined render_page function. Instead, you misspelled and named it as rander_page().
May be you are trying to write:
function render_page()
as you have called it like :
$this->render_page('index');
You have misspelled the function name. Call $this->rander_page('index');

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 - Call to a member function ... on a non-object from Model [duplicate]

This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 9 years ago.
I'm using CI 2.1.3 and I'm having issues with loading models and referring to the CodeIgniter "super object".
For example:
I'm trying to log in using my Login controller:
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Users_model');
}
public function validate_credentials() {
$this->load->library('form_validation');
// Some form validation here...
$query = $this->Users_model->validate();
if($query) { // if the user's credentials validated...
// something
} else {
// something
}
}
And when it gets to my Users_model:
class Users_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function validate()
{
$this->db->where('active', 1);
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('user');
if($query->num_rows == 1)
{
return $query;
}
}
}
I get an error "Fatal error: Call to a member function where() on a non-object in users_model.php on line XX" referring to the first line of validate() function.
I can get it working by using double colon (::) operator in Login controller like Users_model::validate() but I think I shouldn't need that.
I can also get it working by writing something like:
$ci = get_instance();
$ci->db->where...
at the start of the Users_model->validate() function, but I think I shouldn't need to do that either.
The database class is being loaded in autoload.php.
So the problem is that $this in my models refers to the model class itself rather than the "super object" it's supposed to. I have no moves left and I'm guessing it's about something very simple but I just can't see it. Please help me.
Please try by loading CI library like this
$this->load->library('database');
in __construct() function before loading model:
or load this library from autoload file like:
$autoload['libraries'] = array('database','form_validation', 'session');

Pass data from library to controller and then to view in CodeIgniter 2

I'm trying to pass some data from my custom library to the controller and make it available in my view. For the life of me, I can't see where I'm going wrong.
I was going to get database results; for testing I've resorted to a simple array but it's still not working.
The library:
class Test{
public function __construct(){
$this->CI =& get_instance();
}
public function getStuff(){
$test = array('one','two','three');
return $test;
}
}
The controller:
function __construct(){
parent::__construct();
$this->load->library(array('form_validation', 'test'));
}
public function index(){
$data['q'] = $this->test->getStuff();
$this->load->view('index', $data);
}
The view:
var_dump($q); // returns Message: Undefined variable: q
It's late, so this could be a stupid error/typo. This is using CodeIgniter 2.1.0. I've Googled this - none of the solutions seem to work.
Thanks for taking a look!

Resources