Dhara
Dhara

Reputation: 1481

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.

Upvotes: 2

Views: 145

Answers (1)

TimBrownlaw
TimBrownlaw

Reputation: 5507

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.

Upvotes: 3

Related Questions