Codeigniter 控制器和模型的问题
我正在使用 codeigniter 1.7.3,并且已设置并运行控制器,但当我尝试通过控制器访问模型时,无法获取查询结果。
我在配置区域中正确配置了数据库(我知道这一点,因为当我故意弄乱其中一个配置选项时,我收到一条消息,指出无法访问数据库。)
是相关的控制器(SiteObj.php)代码:
class SiteObj extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data['query'] = $this->Site_model->create_site();
if ($data->num_rows() == 1) {
//etc. etc.
这 是相关模型(site_model.php)代码:
class Site_model extends Model {
function __construct() {
parent::Model();
$this->load->database();
}
function create_site(){
$query = $this->db->query("SELECT * FROM sites WHERE siteid = '1' LIMIT 1");
if ($query->num_rows() > 0) {
$row = $query->row();
return $row;
// etc etc
我也尝试将初始函数命名为 Site_model 而不是构造函数,但没有成功。我还尝试通过 autoload.php 文件自动加载数据库,并在 create_site 函数本身而不是构造函数中加载数据库。
这是我收到的错误消息:
Fatal error: Call to a member function num_rows() on a non-object in /www/development/sunrise_ci/00ci00/application/init/siteObj.php 第 9 行
更新:我遵循了第一个响应者给出的建议,但这没有帮助。
此后,我删除了所有对数据库连接的引用,试图找出问题所在。当我只是尝试从控制器内调用 create_site() 函数时,我得到了这样的结果:
Undefined property: SiteObj::$Site_model
Fatal error: Call to a member function create_site() on a non-object
看来问题出在控制器和模型之间,不知何故,它们似乎没有正确地相互“交谈”。有趣的是,我可以看到我可以从 create_site() 将值传递到控制器,但我仍然收到错误消息。
**** 12/18 更新***< em>*******
好的,首先,我修改了 application/config/hooks.php 文件,以便我可以在所有页面调用之前预加载我的初始化代码。所以这个页面有这样的:
$hook['pre_controller'][] = array(
'class' => 'SiteObj',
'function' => '__construct',
'filename' => 'siteObj.php',
'filepath' => 'init'
);
接下来,我有一个默认控制器处理所有页面调用。它位于controllers/page.php,代码如下:
class Page extends Controller {
// I am the core controller for the application.
function _remap() {
$mysite = new SiteObj();
}
}
这会调用我在application/init/siteobj.php 中设置的init 对象。这是代码:
class SiteObj extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->create_site();
if ($data){
$this->siteid = $data->siteid;
} else {
$this->siteid = 0;
}
}
}
最后,这是 models/site_model.php 中的模型代码:
class Site_model extends Model {
function Site_model() {
parent::Model();
$this->load->database();
}
public function create_site(){
// I load the site data from the database and send the result to the controller
$query = $this->db->query("SELECT * FROM sites WHERE siteid = '1' LIMIT 1");
if ($query->num_rows() == 1) {
return $query->row();
}
}
}
我收到此错误:
Undefined property: SiteObj::$Site_model
Fatal error: Call to a member function create_site() on a
non-object in
/www/development/sunrise_ci/00ci00/application/init/siteObj.php
on line 7
提前致谢!
加里
I am using codeigniter 1.7.3 and I have controllers set up and working, but I am having trouble getting back a query result when I try to access a model via a controller.
I have my database configured correctly in the config area (I know this because when I intentionally mess up one of the config options I get a message saying the database cannot be accessed.)
Here's the relevant controller (SiteObj.php) code:
class SiteObj extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data['query'] = $this->Site_model->create_site();
if ($data->num_rows() == 1) {
//etc. etc.
And here is the relevant Model (site_model.php) code:
class Site_model extends Model {
function __construct() {
parent::Model();
$this->load->database();
}
function create_site(){
$query = $this->db->query("SELECT * FROM sites WHERE siteid = '1' LIMIT 1");
if ($query->num_rows() > 0) {
$row = $query->row();
return $row;
// etc etc
I have also tried naming the initial function Site_model instead of constructor, but with no luck. And I also tried auto-loading the database via the autoload.php file, and also by loading the database within the create_site function itself instead of the constructor function.
Here is the error message I am getting:
Fatal error: Call to a member function num_rows() on a non-object in
/www/development/sunrise_ci/00ci00/application/init/siteObj.php on line 9
UPDATE: I followed the advice given by the first responder, but that didn't help.
I have since removed all references to the database connectivity in an attempt to isolate the issue. When I simply try to call the create_site() function from within the controller, I get this:
Undefined property: SiteObj::$Site_model
Fatal error: Call to a member function create_site() on a non-object
So it seems the issue is between the controller and the model, somehow they don't seem to be "talking" to each other correctly. Interestingly, I am able to see that I can pass a value through to the controller from create_site(), but I still get the error messaging along with it.
**** UPDATE 12/18 **********
Ok, first, I have amended the application/config/hooks.php file so that I can pre-load my init code ahead of all page calls. So this page has this:
$hook['pre_controller'][] = array(
'class' => 'SiteObj',
'function' => '__construct',
'filename' => 'siteObj.php',
'filepath' => 'init'
);
Next, I have a default controller handling all page calls. It's located at controllers/page.php and here's that code:
class Page extends Controller {
// I am the core controller for the application.
function _remap() {
$mysite = new SiteObj();
}
}
This calls the init object that I have set up in application/init/siteobj.php. Here's that code:
class SiteObj extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->create_site();
if ($data){
$this->siteid = $data->siteid;
} else {
$this->siteid = 0;
}
}
}
Finally, here's the model code in models/site_model.php:
class Site_model extends Model {
function Site_model() {
parent::Model();
$this->load->database();
}
public function create_site(){
// I load the site data from the database and send the result to the controller
$query = $this->db->query("SELECT * FROM sites WHERE siteid = '1' LIMIT 1");
if ($query->num_rows() == 1) {
return $query->row();
}
}
}
I get this error:
Undefined property: SiteObj::$Site_model
Fatal error: Call to a member function create_site() on a
non-object in
/www/development/sunrise_ci/00ci00/application/init/siteObj.php
on line 7
Thanks in advance!
Gary
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据原始问题中更新的代码进行更新:
请记住,CodeIgniter 的 Hooks 类是在 Loader 类之前初始化的,因此您的 pre_controller 钩子正在实例化
SiteObj
并尝试在通过 Loader 类加载Site_model
之前调用Site_model->create_site()
。它会抛出错误,因为您无法在尚不存在的对象上调用方法。在本例中,
Site_model
是尚不存在的对象。请记住,您可以检查日志文件(位于 /system/logs/ 中)以查看资源的执行顺序。查看 CodeIgniter 应用程序流程图也可能会有所帮助。
希望有帮助!
结束更新
num_rows()
方法只能用于整个$query
结果对象,就像您在您的Site_model
:但是,目前
Site_model
中的create_site()
方法返回单行(return $row;
) 从结果对象中调用,然后您尝试在SiteObj
控制器中的该单行上调用num_rows()
。实际上没有必要这样做,因为
$query->row();
将始终返回单个结果行。如果您确实想从控制器内部调用
num_rows()
(同样,这确实没有意义,因为$query->row()
总是只返回一行),您必须像这样返回整个$query
结果对象:然后在您的
SiteObj
控制器中:希望有帮助 - 如果没有请告诉我。看起来你的代码并不太遥远!作为参考,请查看 num_rows() 方法 和 row() 方法。
Update based on updated code in original question:
Keep in mind that CodeIgniter's Hooks Class is initialized before the Loader Class, so your pre_controller hook is instantiating
SiteObj
and trying to callSite_model->create_site()
beforeSite_model
has been loaded via the Loader Class.It's throwing an error because you can't call a method on an object that doesn't exist yet. In this case
Site_model
is the object that doesn't exist yet.Remember that you can check your log files (located in /system/logs/) to see the order in which resources are executed. It might also be helpful to review the CodeIgniter Application Flow Chart.
Hope that helps!
End Update
The
num_rows()
method can only be used on the entire$query
result object like you have in yourSite_model
:However, currently your
create_site()
method in yourSite_model
is returning a single row (return $row;
) from the result object, and then you're trying to callnum_rows()
on that single row in yourSiteObj
controller.There really is no need to do this because
$query->row();
will always return a single result row.If you did absolutely want to call
num_rows()
from inside your controller (again, there's really no point since$query->row()
will always return only one row), you must return the entire$query
result object like this:Then in your
SiteObj
controller:Hope that helps - if not let me know. It doesn't look like your code is too far off! For reference, check out the num_rows() method and the row() method.