I load the Get_notes model in line 8, but when I want to use Get_notes model to load add_notes method in line 23, the error occur and say Undefined property: Notes::$Get_notes in line 23 !
There is something wrong in line 23 but I dont know what is that.Please help me.
Thanks
<?php
class Notes extends CI_Controller
{
public function index()
{
$this->load->model('Get_notes');
$data['notes'] = $this->Get_notes->get_mm();
$this->load->view('show', $data);
}
public function insert()
{
$title = 'Something';
$text = 'Something else';
$data = [
'title' => $title,
'text' => $text
];
$this->Get_notes->add_notes($data);
}
}
The way you've written it, your Get_notes Model is only available within your index() function.
To make your Get_notes Model available to all functions within a single Controller, load it within that Controller's constructor function...
class Notes extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('get_notes'); // available to all functions within Notes
}
....
To make your Get_notes Model available globally to all functions in any CI Controller, put it in your $autoload['model'] array within the autoload.php file located at application/config/autoload.php...
$autoload['model'] = array('get_notes'); // available to all functions in your CI application
→ Note that it's supposed to be written in all lower-case no matter how you reference it later.
$this->load->model('get_notes');
$this->get_notes->add_notes($data);
See:
Class Constructors
If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it:
parent::__construct(); // Notice there is no dollar sign
Anatomy of a Model:
Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.
Loading a Model:
Your models will typically be loaded and called from within your controller methods. To load a model you will use the following method:
$this->load->model('model_name');
Once loaded, you will access your model methods using an object with the same name as your class:
$this->model_name->method();
Auto-loading Resources
To autoload resources, open the application/config/autoload.php file and add the item you want loaded to the autoload array. You’ll find instructions in that file corresponding to each type of item.
An codeigniter model call is case sensitive therefore to get an model you should use
$this->get_notes->a_function_inside_the_model ();
Also note that the name inside the model folder should always start with a uppercase.
Whenever we run codeigniter on localhost servers like wamp server these problems dont exist but on a live server they will.
Hope this was helpful
Put $this->load->model('Get_notes'); in the insert() function.
If you want to use it globally, put it in a constructor.
Your get_notes model is only loaded in the index function. You cannot load the model in the index function and use it in any other function without having to load it again.
I'm thinking you're trying to load the model once and use it throughout your controller. To do this, you have to load it in the __construct method instead. Your code should be similar to this:
<?php
class Notes extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Get_notes');
}
public function index()
{
$data['notes'] = $this->Get_notes->get_mm();
$this->load->view('show', $data);
}
public function insert()
{
$title = 'Something';
$text = 'Something else';
$data = [
'title' => $title,
'text' => $text
];
$this->Get_notes->add_notes($data);
}
}
Related
I am a beginner in CodeIgniter framework and I have problem with links. I found a couple of pages in which is explained how to link pages, but from some reason I have problem to link two pages. I did this:
Insert Labwork
and that should call controller_proffesor method :
function index(){
$this->load->view('proffesor_view_insert_labwork');
}
i try also this:
Insert Labwork
but when i click on that link this is what i get:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Controller_proffesor::$load
Filename: controllers/controller_proffesor.php
Line Number: 7
Backtrace:
i have also include this line in my autoload file :
$autoload['helper'] = array('url','form');
What am I doing wrong?
Within the constructor have you added:
parent::__construct();
Try adding it inside __construct(). It invokes the constructor of inherited parent class CI_Controller.
Your code should look like this..
class Controller_proffesor extends CI_Controller {
public function __construct()
{
parent::__construct();
//followed by your helper, model load statements
}
NOTE: Refer this https://stackoverflow.com/a/21339891/5176188 for more details. This may be a duplicate question.
Try this:
In your config file you must set something like this:
$autoload['helper'] = array('url','form','html');
In the controller, you must forget to extend it and the name also must concide with the name of your class and it is also like the model extends it to CI_Model
Class Class_Name extends CI_Controller {
public function __construct () {
parent::__construct
// you can load here the session and models
}
}
Hope it helps.
I'm going crazy because I have been using Codeigniter for ages now and I cannot seem to load a model in my view.
This is what I have done. Model code (models/changelog.php):
<?php
class Changelog extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function get_list() {
$query = $this->db->get('changelog');
return $query->result();
}
}
/* Location: ./application/models/changelog.php */
This is my controller (controllers/explore.php):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Explore extends CI_Controller {
public function index() {
$this->load->view('include/header');
$this->load->view('home');
$this->load->view('include/footer');
}
public function changelog() {
$this->load->model('changelog');
$data['changelog_row'] = $this->changelog->get_list();
$this->load->view('include/header');
$this->load->view('explore/changelog', $data);
$this->load->view('include/footer');
}
}
/* Location: ./application/controllers/explore.php */
I get a Codeigniter notice telling me Message: Undefined property: Explore::$Changelog and a PHP error, Fatal error: Call to a member function get_list() on a non-object.
Here is what I did
Tried to autoload the database instead of only loading it in that model
Tried changing the Changelog call in the contorller to lowercase
Checked the connection to the database
Enabled the log file, which doesn't tells me anything new
Everything works correctly, maybe it's just me being a little tired, but if someone could help me out i'd love that :)
After some tests I found out the error is in the
$data['changelog_row'] = $this->Changelog->get_list();
line of my code. I have no idea what the problem is. Case sensitivity is fine (also tried many combinations of lowercase/uppercase), and even if I create another function with different name (e.g. foo()) with a normal echo inside I get the SAME error but referring to that function.
Here is a screen
Something incredible happened: if I add the 'changelog' model in the autoload.php, it seems it can actually load it. What is going on? I used this code already in many applications without a problem.
Another test I did: if I write
public function __construct() {
parent:: __construct();
$this->load->model('changelog');
}
In the controller or just add parent:: __construct(); in the changelog function like
public function changelog() {
parent:: __construct();
$this->load->model('changelog');
$data['changelog_data'] = $this->changelog->get_list();
$this->load->view('include/header');
$this->load->view('explore/changelog', $data);
$this->load->view('include/footer');
}
It works O_o. But why? I changed the topic title, now everything is around my inability to load my model in the controller's functions.
CI is acting a bit strange with that.
Om unix systems (where filenames a case sensitive) you have to load the model with case senstive names.
However in the controller you should address it with lowercase, i.e
$this->load->model('changeLog');
$this->changelog->getList();
Please change your changelog function with following syntax.
public function changelog() {
$this->load->model('changelog', 'model');
$data['changelog_row'] = $this->model->get_list();
$this->load->view('include/header');
$this->load->view('explore/changelog', $data);
$this->load->view('include/footer');
}
And your model's function should be as follows:
public function get_list() {
$query = $this->db->get('changelog');
return $query->result();
}
You miss the public keyword. So, it is a private function now that's why you can't access it from another class.
You are using the same name for the model as for the controller function so getting name clash because;
$this->changelog()
Is trying to refer to the model you have loaded and the class method at the same time.
Rename your Explore::changelog() function (or model name - suffix _m for example) and should be fine. Something like;
$this->changelog_m->getList();
After many hours of test and messing up with everything, INCLUDING configuration files... I DID IT!
It was something totally above models and controllers, it was something about the hooks I called. In fact, I have a hook called languageloader.php written like this:
class LanguageLoader extends CI_Controller {
public function initialize() {
$ci =& get_instance();
$site_lang = $ci->session->userdata('site_lang');
if (!empty($site_lang)) {
$ci->lang->load('text', $site_lang);
} else {
$ci->lang->load('text', 'english');
}
}
}
In my hooks file it was loaded like:
$hook['post_controller_constructor'] = array(
'class' => 'LanguageLoader',
'function' => 'initialize',
'filename' => 'languageloader.php',
'filepath' => 'hooks',
);
Since I was using post_controller_constructor, referring to CI doc files it is
Called immediately after your controller is instantiated, but prior to
any method calls happening.
I believe that doing something like my $ci =& get_instance(); I couldn't instance my damn model. I fixed it by changing the hook to
$hook['pre_controller']
I didn't think it could have been something about the hooks and that was the reasons why I didn't post it. Thanks to everyone who tried to help me out in the while. I hope this helped someone else who was in my same trouble!
For your reference, you can modify your autoload.php from
./application/config/autoload.php
then just include all you models exist on your model folder
./application/models/*_Model.php
So from autoload.php set your autoload['model'] array.
/*
| -------------------------------------------------------------------
| Auto-load Models
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['model'] = array('first_model', 'second_model');
|
| You can also supply an alternative model name to be assigned
| in the controller:
|
| $autoload['model'] = array('first_model' => 'first');
*/
$autoload['model'] = array('Foo_Model', 'Bar_Model', ...);
Then from your Controller you can remove the $this->load(...); from your __construct(), since all Models you defined from autoload.php is loaded Globally and can be use whenever you run your system.
Then just call directly the public methods you need from that Model, like so;
$this->Foo_Model->your_public_method_to_call();
PS:
CI_VERSION: 3.x
PHP: 5.4+
I hope this helps somebody, thanks.
I have Content controller with REST methods (index..create..store..) and i want to run some code before any of those methods run.
what i am trying to do is to set var for my layout with some data that is relevant to all my methods within Content controller:
$this->layout->myvar = 'some-data';
I tried to do something like that:
class ContentController extends BaseController {
function __construct() {
$this->layout->myvar= 'some-data';
}
..
but it doesn't seems to work.
i get "Attempt to assign property of non-object" error.
Laravel 5.1+
This has been deprecated in favour of Middleware.
Laravel 4
You could set the beforeFilter like this:
class ContentController extends BaseController {
function __construct() {
// this function will run before every action in the controller
$this->beforeFilter(function()
{
// this will make the variable $myvar available in your view
$this->layout->with('myvar', 'some-data');
});
}
}
try share in app/routes.php
View::share('variable_name', 'value');
ex:
View::share('name', 'Steve');
will share variable with its value across all views
Normally I load model inside classes like this:
public $uses = array('Table1', 'Table2', 'Table3');
But some of my models are only used by 2-3 actions. So I don't want to load that model for other actions. So I need to declare "table1" controller wide. And load "table2" and "table3" when I need them inside controller. Is it possible? I couldn't find inside cookbook.
Something like this:
class myController extends Controller {
public $uses = array('Table1');
public function myaction() {
$uses = array('Table2','Table3');
....
}
}
CakePHP uses lazy loading since v2.x, i.e. if you specify a model in the $uses array it is only loaded when it is really used, so it's fine to use your first snippet.
But if necessary you can also use the loadModel() method to load a model in a single action:
public function myaction() {
$this->loadModel('ModelName');
$this->ModelName->doSomething();
...
}
In any controller method you can import models when required:
App::Import('Model', 'YourModel');
$foo = new YourModel();
Now you can call methods by referencing from the model Variable e.g.
$foo->myMethod()
Instead of:
$this->Model->myMethod();
Should I not be using Index as the name for a controller class in CodeIgniter? I have an Index controller, and I'm seeing its methods being called multiple times. More specifically, I always see its index method called first, whether or not I'm visiting a path that should be routed there.
In application/controllers/index.php
class Index extends CI_Controller
{
public function index()
{
echo "index";
}
public function blah()
{
echo "blah";
}
}
When I visit index/blah, I see indexblah printed. When I visit index/index, I see indexindex. If I rename the controller to something else (e.g. Foo), it doesn't have a problem. That's the obvious workaround, but can anyone tell me why this is happening? Should I report this as a bug to CodeIgniter?
(Notes: I have no routes set up in configs/routes.php; my index.php is outside the CodeIgniter tree)
To further clarify what the issue is, in PHP4 Constructors were a function that had the same name as the Class...
example
class MyClass
{
public function MyClass()
{
// as a constructor, this function is called every
// time a new "MyClass" object is created
}
}
Now for the PHP5 version (Which codeigniter now, as of 2.0.x, holds as a system requirement)
class MyClass
{
public function __construct()
{
// as a constructor, this function is called every
// time a new "MyClass" object is created
}
}
So To answer the question that addresses the problem...
Should I not be using Index as the name for a controller class in CodeIgniter?
I believe it would be best to not choose Index as a controller name as the index() function has a reserved use in codeigniter. This could cause issues depending on your PHP configuration.
can anyone tell me why this is happening?
When your controller get's instantiated, index as the constructor is getting called.
Compare Constructors and DestructorsDocs:
For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class . [highlighting by me]
In your case your Controller does not have any __construct() function but a function that has the same name as the class: index. It is getting called in the moment Codeigniter resolves and loads and then instantiates your Index Controller.
You can solve this by just adding the constructor to your Controller:
class Index extends CI_Controller
{
public function __construct() {}
public function index()
{
echo "index";
}
public function blah()
{
echo "blah";
}
}
After this change, it does not happen again.
Should I report this as a bug to CodeIgniter?
No, there is not really a need to report this as a bug, it's how the language work and as Codeigniter supports PHP 4 it must remain backwards compatible and needs to offer PHP 4 constructors. (Note: The Codeigniter project documents, they need server support for PHP version 5.1.6 or newer, but the actual code has PHP 4 compatiblity build in, I'm referring to the codebase here, not the documentation.)
Here is another solution using Codeigniter3
require_once 'Base.php';
class Index extends Base
{
public function __construct()
{
parent::index();
$classname=$this->router->fetch_class();
$actioname=$this->router->fetch_method();
if($actioname=='index' || $actioname == '')
{
$this->viewall();
}
}
}
And the viewall() had the following
$this->siteinfo['site_title'].=' | Welcome';
$this->load->view('templates/header', $this->siteinfo);
$this->load->view('templates/menu', $this->siteinfo);
$this->load->view('index/viewall', $data);
$this->load->view('templates/footer', $this->siteinfo);
The Base controller does all the library and helper loading for the entire application which is why it is being required in the default class
Basically from my short understanding of CodeIgniter, having a default action as index is a wrong. I found this out by using the printing the result of $this->router->fetch_method(); in the construct() of my index class. The default action by CodeIgniter is index, you may only set the default controller within application/config/routes.php and not the default action.
So my advice, never use index() as the default action especially if you are using index as the default controller