I searched for solutions , but all answers i found was outdated and don't helped me. So , there is my problem.
I'm trying to extend CI_Model core model class. I'm following the oficial guide
But it's not working.
Below is how i'm doing it:
Path/Filename of my class i want to create:
application/core/MY_Model.php
The content of MY_Model.php
class MY_Model extends CI_Model {
public function __construct()
{
parent::__construct();
}
}
And, this is how i'm trying to load it at my model file:
class Setor extends MY_Model { ....
The error message i'm getting:
Class 'MY_Model' not found in C:\wamp\www\agenda\application\models\Setor.php
I'm using Windows 10, php 5.5.12, CodeIgniter 3.0.6
Related
on the server I can not load a model that is in another module in the HMVC, framework of the Codeigniter framework, I tried this way:
Class Contract extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Contract_model', 'contract');
$this->load->model('register/customer_model', 'customer');
$this->load->model('register/daydue_model', 'due');
}
}
Structure
In the localhost works quietly, since the server is giving this error::
Message: Unable to locate the model you have specified: Daydue_model
When you uploading on server use casesensitive it does not create problem on localhost. your model name is incorrect.
Do it like this
$this->load->model('register/daydue_model', 'due');
And rename your model name to Daydue_model and check the class name same is the name of model like:
class Daydue_model extends CI_Model
I was creating objects of a class in Codeigniter core classes which I will extend in my controllers but there seems to be problem with loading model after object is created. Here is what I did
Core File:
require_once('folder/systemize.php'); // class systemize is in this file
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$systemize = new systemize();
$systemize->init();
}
After this object is initialized, loading model will not work
Controller file which extends MY_Controller
class Log extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('access_model', 'TEST');
$this->TEST->foo('param1', 'param2'); //function inside access_model
}
It will give me these errors
A PHP Error was encountered
Severity: Notice Message: Undefined property: Log::$TEST
Fatal error: Call to a member function foo() on a non-object
These errors are solved if I don't create object of systemize. Can anyone tell me the how to solve this problem?
EDIT: Systemize File
class Systemize extends CI_Controller
{
function __construct() {
parent::__construct();
}
public function init() {
if('2' == '3') // false for now so it does nothing
{
$this->load->view("system/view_file");
exit();
}
}
Php error indicates on Log File where TEST->foo is called
If I Load Model before creating object, it works but not vice-versa. I still don't understand why is Codeigniter behaving like this?
Ok after going through system files of Codeigniter and printing and echoing inside them I found the source of problem. Now the behavior of CI_Controller is that when we extend CI_Controller, it creates object of Controller and Model and stores it with reference to the pointer.
Now what I did is that I extended systemize with CI_Controller.
MY_Controller extends CI_Controller which means $obj is created storing pointer to all objects of CI which will be used.
But when I manually created object of systemize extending CI_Controller, it has created object of only Controller and not Model and replaced the $obj.
That is why loading model will cause problem since $obj doesn't have Model Object in it.
The solution the problem I found is I did not create object of systemize in MY_Controller. Instead I extended MY_Controller with Systemize and it started working flawlessly.
Another solution is if you don't need CI functionality in your class you can create object of class and not extend it with CI_Controller.
I have a relatively simple question. I am trying to inherit a constructor from a php superclass to authenticate on this controller.
Here is my super class:
class Auth_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
if(!session_id()){
session_start();
}
$this->load->view('login_v/logincheck');
}
}
and here is my subclass:
class Event_Controller extends Auth_Controller {
function __construct()
{
parent::__construct();
}
public function get_events_by_owner() {
$this->load->model('Event_model');
$data['events'] = $this->Event_model->select_by_owner($_SESSION['SignedIn']);
$this->load->view('event_view', $data);
}
}
This is not working. only a white page is rendered. I'm not sure why it isn't working. If I move the constructor from Auth_Controller to Event_Model this works.
Thanks!
EDIT:
Fatal error: Class 'Auth_Controller' not found in
../controllers/event_controller.php on line 12
Your solution is not going to work, you should try either:
Move this code:
if(!session_id()){
session_start();
}
$this->load->view('login_v/logincheck');
to a cutom library, than run this library within constructor of your controller. Please read Creating Libraries for details.
or:
Create MY_Controller class and put auth code (quoted above) into its constructor. Than you'll be able to extend like:
class Event_Controller extends MY_Controller {
(...)
Please read Creating Core System Classes for details.
I know there are other similar questions, but even after following all the syntax, this errror exists
I am using Codeigniter version 2.1.4 on wamp server, windows 7.
in application/core/ i have MY_Basedbclass.php starting with
abstract class MY_Basedbclass extends CI_Model
{
and trying to use it in my mode like
class Accounts_manager extends MY_Basedbclass
{
ending up with the error - Fatal error: Class 'MY_Basedbclass' not found in C:\wamp\www\party_app\server\application\models\accounts_manager.php
Please suggest what i am doing wrong. Thanks
EDIT - Addign a screen shot for the file structure sake
why you define your core model class as abstract class? after that you can only set core model class as MY_Model. this name get from subclass_prefix + class_name_you_want_to_over_write
define your core class like this:
class MY_Model extends CI_Model {
// your code
}
before that assured in your config.php file you set $config['subclass_prefix'] = to MY_
config file address: application > config > config.php
I have strictly followed the how-to article by Phil Sturgeon, to extend the base controller. But I get still some errors.
My 3 classes:
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
// application/libraries/Public_Controller.php
class Public_Controller extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
// application/controllers/user.php
class User extends Public_Controller{
public function __construct(){
parent::__construct();
}
}
Fatal error: Class 'Public_Controller' not found in /srv/www/xxx/application/controllers/user.php on line 2
Curious is that the following snippet is working, if I directly extends from MY_Controller:
// application/controllers/user.php
class User extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
I have loaded the controllers via __autoload() or manually. The controllers are loaded succesfully.
CI-Version: 1.7.3
You need to require the Public Controller in your MY_Controller
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
require(APPPATH.'libraries/Public_Controller.php');
You get the error because Public_Controller was never loaded. Doing this would allow you to extend from Public_Controller
I like what you are doing because I do that all the time.
You can do this also in your MY_Controller when you want to create an Admin_Controller
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
require(APPPATH.'libraries/Public_Controller.php'); // contains some logic applicable only to `public` controllers
require(APPPATH.'libraries/Admin_Controller.php'); // contains some logic applicable only to `admin` controllers
You should place Public_controller in with MY_Controller inside MY_Controller.php
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
class Public_Controller extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
I use __construct everywhere and it works fine I recently wrote up an article on how to do this in relation to wrapping your auth logic into your extended controllers. It's about half way down when I start discussing constructing your controllers.
Problem was solved here: http://devcrap.net/pl/2011/09/04/codeigniter-dziedziczenie-z-my_controller-extends-my_controller/. In polish but code is good :]
I had problem like this,After some search I found error was made myself,Because my controller class name was MY_Controller but file name was My_Controller[Case not matching].
Note:- In localhost I didnt have any error.
In extended controller I Use
class Home extends MY_Controller{
function __construct() {
parent::__construct();
}
}
even I got the error.
After changing my file name to MY_Controller it started to work well.
I have a custom controller class called MY_Controller it extends CI_Controller and it works fine. It is located at application/core and it has custom functions lo load views in my site.
I use an abstract class My_app_controller that extends MY_Controller for my_app specific behabior, I want every controller in my_app to extend this abstract class. (I use diferent apps in the site, so some apps will extend My_app_controller and other apps will extend My_other_apps_controllers)
But when I try to extend My_app_controller from any controller in my application, "Main_app_controller extends My_app_controller" generates a Class 'My_app_controller' not found exception.
I found two solutions:
use include_once in Main_app_controller.php file.
include_once APPPATH.'controllers/path/to/My_app_controler.php';
break the "one class per file" rule of code igniter and define my My_app_controller just in the same file MY_Controller is (under application/core).
Manual says:
Use separate files for each class, unless the classes are closely
related
Well... they are.
Anyway, I prefered to use the include_once solution as I think it is better to have one file per class and My_app_controller is located under application/controllers/my_app folder. (so application/controllers/other_apps will exist)