Codeigniter:多级模型扩展不起作用。收到错误“找不到课程”

时间:2018-02-28 08:09:08

标签: php inheritance codeigniter-3 multi-level

我正在尝试在模型中应用多级扩展。

请参阅下面的代码。

我有一个模型“Order”,它扩展了CI的核心模型

Class Order extends CI_Model {
   function __construct() {
    parent::__construct();
   }
}

现在我正在从“订单”模型

创建新的“Seller_order”模型
Class Seller_order extends Order {
    function __construct() {
       parent::__construct();
   }
}

现在我在控制器中加载“Seller_order”模型。

class Seller_order_controller extends CI_Controller { 
        function __construct() {
        parent::__construct();
        $this->load->model('Seller_order');
    }
}

在加载时,我收到以下错误:

Fatal error: Class 'Order' not found

请帮助。 我是否需要首先加载“订单”模型然后“Seller_order”? 我认为如果我扩展它,我不需要加载“订单”模型。

1 个答案:

答案 0 :(得分:2)

我不打算用很多词来包装它,希望代码本身可以解释所需的内容。

我添加了一些Debug echo来帮助显示事情是如何运行的,这是我用“玩”来做的事情。

我将假设以下布局......不是你拥有它,所以你必须改变它以适应。

application
 -> controllers 
     -> Seller_order_controller.php
 -> models
     -> Order.php
     -> Seller_order.php

控制器 - 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();
    }
}

型号 - 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>";
    }
}

模型 - 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>';
    }
}

作为旁注:不确定为什么要扩展这样的模型。通常的规则是每个模块都有自己的模型。 我从来不需要这样做,但如果我这样做,现在我知道如何。