I am trying to port an application's few functions from a CodeIgniter application to another existing CodeIgniter application. Both applications on themselves are working very well but when I added this thing it gives the following error:
Fatal error: Call to a member function order_by() on null in …\application\core\MY_Model.php on line 7
In this question I have removed parts unrelated to the error to simplify code.
//MY_Model.php model file
<?php
class MY_Model extends CI_Model {
protected $_order_by = '';
public function get(){
$this->db->order_by($this->_order_by);
}
}
//article_m.php model file
<?php
class Article_m extends MY_Model
{
protected $_order_by = 'pubdate desc, id desc';
}
//frontend.php controller file
<?php
class Frontend extends MY_Controller
{
function __construct()
{
$this->load->model('article_m');
}
function index()
{
$this->article_m->get();
}
}
Please help. Thank you!
whenever calling any $this->db ... you have to make sure to load your database library. Check in application\config\autoload.php for the following:
$autoload['libraries'] = array('database');
Related
Hello I'm currently facing a problem of adding another controller and the problem is that
I have 2 controllers
class 1st_Controller extends CI_Controller {
}
and
class 2nd_Controller extends CI_Controller{
my 2 models are working perfectly fine and the only problem is that I need to call 1 model per each controller
for example
1st controller is for 1st model and 2nd controller is for 2nd model.
Now what I've tried so far is
class 2nd_Controller extends 1st_Controller {
public function __construct()
{
header("Access-Control-Allow-Origin: *");
parent::__construct();
$this->load->model('2nd_model','2ndmodel');
$this->load->helper('url');
$this->load->library("pagination");
$this->load->library("session");
}
public function index()
{
$data['title'] = 'System Login';
$get_all_inv = $this->2ndmodel->get_all();
$data["tryvariable"] = $get_all_inv;
$this->template->load('default_layout','contents','myview2nd', $data);
}
}
I tried echo in my view like this
<?php echo $tryvariable; ?>
but no luck because the error says that it is an undefined variable .
Your second controller can't be define cause it's not define as subclass_prefix in your codeigniter application.
class 2nd_Controller extends 1st_Controller { //codeigniter don't recognize this.
}
The simpliest way to solve your problem is.. the fact that you can call multiple models in 1 Controller.
so you can have:
class 1st_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('1st_model','1stmodel');
$this->load->model('2nd_model','2ndmodel');
}
}
or call just once.
class 2nd_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('2nd_model','2ndmodel');
}
}
Hope that helps.
So what I did was write like this on my route to have my 2nd_Controller working
$route['default_controller'] = '1st_Controller';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['2nd_Controller'] = '2nd_Controller';
Now everything is working perfectly. Thanks for all the help
In CodeIgniter 3.1.3 I extended CI_Model in application/core/MY_Model.php:
class MY_Model extends CI_Model {
public function __construct($id = NULL) {
parent::__construct();
$this->load($id);
}
public function load($id) {
$this->db->where('id', $id);
$this->db->limit(1);
$query = $this->db->query($this->_table);
if ($row = $query->result()) {
// #todo Process results
}
// Free the resources.
$query->free_result();
}
}
My User_model looks like this:
class User_model extends MY_Model {
public function __construct($id = NULL) {
parent::__construct($id);
}
}
I also extended the CI_Controller in application/core/MY_Controller as follows:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('User_model');
}
}
I autoloaded the database connection in application/config/autoload.php as:
$autoload['libraries'] = array('database');
Without loading the User_model in the controller I was able to run migrations, so the database connection is configured correctly. But when I added $this->load->model('User_model') I get the error "Undefined property: User_model::$db".
If I let User_model extend CI_Model it runs without errors and with a var_dump in the homepage's controller it shows that the database is autoloaded correctly. But as soon as I put MY_Model in between, the database class is undefined in the model and also $this->load in the model returns NULL, so it appears the model is not properly constructed.
I can only imagine this to be a very small mistake, but I've been staring at it for hours with several breaks in between and I just don't see it. Can anyone else help me?
From a controller, lets say A_controller
$this->load->library('MyLib');
In MyLib, my original issue is this block
$ci = &get_instance();
$ci->load->model('my_model');
$this->active_model = $ci->my_model;
Then the miracle come when it become
$ci = &get_instance();
$ci->load->model('my_model');
$ci->my_model->db = & $ci->db;
$this->basic_model = $ci->my_model;
When CI loads a model $db property come along with CI.
So, when you call $this->db in model which means you want "my_model" have the property, right?
What I did just link the property from CI to my_model!!!
i am trying to make a core class in my codeigniter but its giving error that Unable to locate the specified class: Loader.php.
My Core class is MY_base.php and code is.
class MY_base extends CI_Controller{
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
My model Mod_practice.php code is
class Mod_practice extends CI_Model{
public function get_header(){
$query = $this->db->get('header');
$result = $query->result_array();
return $result;
}
}
My home.php ( main controller) code is
class Home extends MY_loader{
function index(){
parent::MY_base();
}
}
but when i try to run Home controller its giving me the following error
Unable to locate the specified class: Loader.php.
Where can be the error ? Thanks in advance.
You are doing something wrong. You need to construct CI_controller first.
class MY_base extends CI_Controller{
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
And now you can do this:
class Home extends MY_base{
public function __construct()
{
parent::__construct();
// Here you have access to load_header() function
}
}
Ou - and also your create MY_base and then refer to MY_loader.
may bee you can use HMVC Codeigniter https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
I have this file structure:
application
controllers
home.php
core
MY_Controller.php
models
users.php
These are the codes for each file:
home.php
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
echo "Hello World!";
}
}
MY_Controller.php
class MY_Controller extends CI_Controller {
var $user_model = null;
public function __construct() {
parent::__construct();
$this->user_model = new Users();
}
}
users.php
class Users extends CI_Model {
public function __construct() {
parent::__construct();
}
}
When I load the webpage on a browser, I get this error:
Fatal error: Class 'Users' not found in C:\%path%\application\core\MY_Controller.php on line 7.
Please help me make my custom base Controller to find my Model. Thank you!
NOTE:
The cases are as I have provided them (I heard there might be issues with case-sensitivities).
EDIT:
When I opened the log files, I found this.
ERROR - 2014-02-17 01:02:05 --> Severity: 8192 --> mysql_pconnect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead C:\%path%\system\database\drivers\mysql\mysql_driver.php 91
Nevermind, I got it to work by either going in the config.php file and autoloading the model or by doing $this->load->model('Users');
Upper or lower case U seems to work.
My CI version is CI2.3. I'm running this php code in my local host. I followed all the steps given there but I'm getting this error don't know why? and I changed Controller to CI_Controller. Hello world Program worked finely. This link code is not working. please need help!
you should extend model like this in codeIgniter
class Modelname extends CI_Model
{
function __construct()
{
parent::__construct();
}
}
Well actually the study guide is of old version of CI, where you used to extend your models from Model class as show in the guide. But now it has been changed. Now you have to extend it from CI_Model, same goes for Controller.
For controllers
class Employee_controller extends CI_Controller
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
and for models:
class Employee_model extends CI_Model
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
Use like this
<?php
class Employee_model extends CI_Model
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
You must create a model in the model folder like my_model.php
And create the class like
class My_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
}
Remember the class and the file should be same.
Docs http://ellislab.com/codeigniter/user-guide/general/models.html