Fatal error: Class 'Model' not found in CodeIgniter - php

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

Related

exists, but doesn't declare class Code Igniter

I try to load this model named "m_cart", but it doesn't work on my controller. I got an error here "M_cart.php exists, but doesn't declare class M_cart"
Controller Part.php
<?php
class Cart extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('M_cart');
$this->load->session('cart');
}
Model m_cart.php
<?php
class Cart_model extends CI_Model{
function get_all_produk(){
$hasil=$this->db->get('produk');
return $hasil->result();
}
Controller name should be Cart.php not Part.php if you are declaring class Cart(don't forget to extend CI_Controller).
The m in m_cart should be capitalized both in the file name M_cart.php and declaration and should extend CI_Model e.g. class M_cart extends CI_Model.
If both fail to work, verify verify verify that you are editing the same copies as you are testing .etc.
Your model must be declared as a class that extends CI_Model
Class M_cart extends CI_Model
{
function get_all_produk()
{
$hasil=$this->db->get('produk');
return $hasil->result();
}
function some_other_model()
{
// some other thing
}
}
load->model('cart_model');
$this->load->session('cart');
}
//this is where your problem is coming from, you did not put the right model name. you used m_cart instead of cart_model//

Codeigniter: Multilevel model extends not working. Getting error "Class not found"

I am trying to apply multilevel extends in models.
See my below code.
I have one model "Order", which extends CI's core model
Class Order extends CI_Model {
function __construct() {
parent::__construct();
}
}
Now I am creating new "Seller_order" model from "Order" model
Class Seller_order extends Order {
function __construct() {
parent::__construct();
}
}
Now when i am loading "Seller_order" model inside controller.
class Seller_order_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Seller_order');
}
}
At the time of loading i am getting following error:
Fatal error: Class 'Order' not found
Help please.
Do i need to load first "Order" model then "Seller_order"??
I think i don't need to load "Order" model if i am extending it.
I'm not going to wrap this in a lot of words, hoping the code itself might explain what is needed.
I've added in some Debug echo's to help show how things run, which I did as I "played" with this to figure it out.
I'll assume the following layout... Not as you have it, so you'll have to change it to suit.
application
-> controllers
-> Seller_order_controller.php
-> models
-> Order.php
-> Seller_order.php
Controller - Seller_order_controller
class Seller_order_controller extends CI_Controller {
function __construct() {
parent::__construct();
echo "construct(): I am the <b>Seller Order Controller</b> Constructor<br>";
$this->load->model('seller_order');
}
public function index() {
echo "This worked";
echo '<br>';
echo $this->seller_order->show_order();
}
}
Model - Seller_order.php
require APPPATH.'models/Order.php';
Class Seller_order extends Order {
function __construct() {
parent::__construct();
echo "construct(): I am the <b>Seller Order</b> Constructor<br>";
}
}
Model - Order.php
Class Order extends CI_Model {
function __construct() {
parent::__construct();
echo "construct(): I am the <b>Order</b> Constructor<br>";
}
public function show_order() {
echo "This is showing an Order";
echo '<br>';
}
}
As a side note: Not sure why you would want to extend models like this. The usual rule is each module has it's own model(s).
I've never needed to do this, but if I ever do, now I know how.

Codeigniter Error Unable to locate the specified class: Loader.php

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

Extends Model in CodeIgniter

Well, I'm newbie in CodeIgniter Framework and i'm trying building a generic Model Class. See:
class Basic_Model extends CI_MODEL {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
I want to extends all Models based on Basic_Model, like this:
class Pagina_Model extends Basic_Model {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
The problem is when i try to call "pagina_model" from a Controller a got the following error:
Fatal error: Class 'Basic_Model' not found in /var/www/myproject/application/models/pagina_model.php on line 12
If i use "basic_model" in controller everything works fine.
EDIT 1:
I created a file named MY_Basic_Model.php in "/application/core" and changed the class name to "MY_Basic_Model". But i got the error:
Fatal error: Class 'MY_Basic_Model' not found in /var/www/myproject/application/models/pagina_model.php on line 12
For this you have to create Core System Classes (this is also known as Method Overriding).
Create a file MY_Model.php in application/core/ directory which will extend the base CI_Model class:
<?php
class MY_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Now you can extend this in your models (../applicaton/models/):
<?php
class Pagina_Model extends MY_Model {
function __construct()
{
parent::__construct();
}
}
Few things to note here:
1) The class declaration must extend the parent class.
2) Your new class name and filename must be prefixed with MY_ (this item is configurable).
How to configure:
To set your own sub-class prefix, open your application/config/config.php file and look for this item:
$config['subclass_prefix'] = 'MY_';
Documentation:
https://ellislab.com/codeigniter/user-guide/general/core_classes.html
You can do it this way.
lets assume you have basic_model.php inside your model folder .
Now add the code for class Basic_Model that you have written
class Basic_Model extends CI_MODEL {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
Now make a pagina_model.php inside your model folder and add the code that you written. Just include the first line like bellow
<?php
require APPPATH.'/models/basic_model.php';//just add this line and keep rest
class Pagina_Model extends Basic_Model {
function __construct()
{
parent::__construct();
}
}
hope this will solve your problem
You can do this ... MY_ model is really good but should you wish a sub model to extend a different sub model you can always do:
require(APPPATH.'models/Other_model.php');
class New_model extends Other_Model {
In my instance Other_Model actually extends MY_model.

Unable to locate CodeIgniter model

I'm trying out codeigniter for the first time on a MAMP deployment however I'm unable to load a model into a controller. The end result is always...
Unable to locate the model you have specified: second_model
Controller - welcome.php
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('second_model');
$this->load->view('welcome_message');
}
}
Model - second_model.php
class Second_model extends Model {
public function __construct()
{
// model constructor
parent::__construct();
}
}
You should be extending CI_Model rather than Model
So:
class Second_model extends CI_Model {
public function __construct()
{
// model constructor
parent::__construct();
}
}

Categories