我正在尝试在模型中应用多级扩展。
请参阅我的下面的代码。
我有一个模型“Order”,它扩展了 CI 的核心模型
Class Order extends CI_Model {
function __construct() {
parent::__construct();
}
}
现在我正在从“Order”模型创建新的“Seller_order”模型
Class Seller_order extends Order {
function __construct() {
parent::__construct();
}
}
现在,当我在 Controller 内加载“Seller_order”模型时。
class Seller_order_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Seller_order');
}
}
在加载时我收到以下错误:
Fatal error: Class 'Order' not found
请帮忙。 我需要先加载“Order”模型,然后加载“Seller_order”吗? 我认为如果我要扩展它,我不需要加载“Order”模型。
最佳答案
我不会用很多文字来概括这一点,希望代码本身可以解释需要什么。
我添加了一些调试回显来帮助显示事情是如何运行的,当我“玩”它来弄清楚它时,我就这样做了。
我将假设以下布局...与您的布局不同,因此您必须更改它以适应。
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();
}
}
模型 - 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>';
}
}
旁注:不确定为什么您想要扩展这样的模型。通常的规则是每个模块都有自己的模型。 我从来没有需要这样做,但如果我需要这样做,现在我知道该怎么做了。
关于php - Codeigniter : Multilevel model extends not working. 获取错误 "Class not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49024732/