简体   繁体   English

Codeigniter 应用程序返回“致命错误:在实时而非本地调用非对象 in”上的成员函数 result()

[英]Codeigniter App Returns `Fatal error: Call to a member function result() on a non-object in` on live but not local

I'm new to codeigniter and also to MVC approach to OOP so I am already resigned to the fact that this is a mistake I have made and not likely to be a issue outside of my own code.我是 codeigniter 的新手,也是面向 OOP 的 MVC 方法的新手,所以我已经接受了这样一个事实,即这是我犯的一个错误,不太可能是我自己的代码之外的问题。

I have a controller (Purchase_Order) which loads 2 Models (Product_m & Supplier_m) + others.我有一个控制器 (Purchase_Order),它加载 2 个模型 (Product_m & Supplier_m) + 其他。

I have built a custom MY_Model which takes 0 params and generates a query based on the model settings (table_name, primary_key etc...).我构建了一个自定义 MY_Model,它采用 0 参数并根据模型设置(table_name、primary_key 等...)生成查询。

In my local instance (Wamp) it works perfectly.在我的本地实例 (Wamp) 中,它运行良好。 As soon as I upload to the Live web server (Linux) I get the following error;一旦我上传到 Live web 服务器 (Linux),我就会收到以下错误;

Fatal error: Call to a member function result() on a non-object in (MY_Model.php on line 58) . Fatal error: Call to a member function result() on a non-object in (MY_Model.php on line 58)

I have another Controller (User) which loads the Model (User_m) and passes via the same MY_Model route and that works perfectly online and locally.我有另一个控制器(用户),它加载模型(User_m)并通过相同的 MY_Model 路由传递,并且可以在线和本地完美运行。

I can only assume I have a scope issue or something similar but I have spent the last 48hrs obsessing over fixes to no avail.我只能假设我有一个范围问题或类似的问题,但我在过去的 48 小时内一直沉迷于修复无济于事。

I really hope someone can spare the time to enlighten me on this subject.我真的希望有人能抽出时间在这个主题上启发我。

MY_Model MY_型号

class MY_Model extends CI_Model {    
//Vars
protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $_rules = array();
//protected $_timestamps = FALSE;

//Constructor
function __construct() {
    parent::__construct();
}



// GET
public function get($id = NULL, $single = FALSE) {

    if($id != NULL){
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->where($this->_primary_key, $id);
        $method = 'row';
    }
    elseif($single == TRUE){
        $method = 'row';
    }
    else{
        $method = 'result';
    }

    if(!count($this->db->ar_orderby)){
        $this->db->order_by($this->_order_by);
    }

    //echo var_dump($this);
    return $this->db->get($this->_table_name)->$method();
    //die('<pre>'.print_r($this->db, true).'</pre>');
}

Other Classes Removed as they were not part of the issue.其他类被删除,因为它们不是问题的一部分。

Many Thanks to all but ultimately @rojoca as the pertinent question: Looks like $this->db->get($this->_table_name) is returning null. Do you have $db['default']['db_debug'] set to FALSE? It's possible you have some database connection error.非常感谢所有人,但最终 @rojoca 作为相关问题: Looks like $this->db->get($this->_table_name) is returning null. Do you have $db['default']['db_debug'] set to FALSE? It's possible you have some database connection error. Looks like $this->db->get($this->_table_name) is returning null. Do you have $db['default']['db_debug'] set to FALSE? It's possible you have some database connection error. which led me to the answer;这让我得到了答案;

I had previously exported the live DBase to a local instance to test which worked fine which naively led me to believe this was not where the problem was.我之前曾将实时 DBase 导出到本地实例以进行测试,该实例运行良好,这让我天真地相信这不是问题所在。

I was using a different database config setup for the live site which did have db_debug set to FALSE.我为实时站点使用了不同的数据库配置设置,它确实将 db_debug 设置为 FALSE。 Turning it back on quickly identified that the web server/service I am using had forced the table names to lowercase.重新打开它很快就发现我正在使用的 Web 服务器/服务强制表名小写。 I was completely unaware of this and I have never heard of this forcing approach before either.我完全不知道这一点,我以前也从未听说过这种强制方法。

Lesson learnt for me.给我的教训。 Thanks again for all assistance on this issue.再次感谢您在此问题上的所有帮助。 All credit should go to @rojoca for this answer.这个答案的所有功劳都应该归功于@rojoca。 Thank you - time for sleep :)谢谢-该睡觉了:)

When this happens, it means the codeigniter could not find a field in your database table (this has happened several times to me).发生这种情况时,意味着 codeigniter 无法在您的数据库表中找到字段(这在我身上发生过多次)。 The solution would theoretically create the fields that are missing.该解决方案理论上会创建缺失的字段。

To resolve this error, I use a small solution:为了解决这个错误,我使用了一个小解决方案:

 <?php

   public function test($op)
    {
        $this->db->where('op', $op);
        $result = $this->db->get('test');
        return ($result instanceof CI_DB_mysql_result) ? $result->result() : false;
    }

Here we check if the variable is an instance of CI_DB_mysql_result.这里我们检查变量是否是 CI_DB_mysql_result 的实例。 This error happens because the method does not return the instance of this class!!!出现这个错误是因为该方法没有返回这个类的实例!!!

Sorry, I do not speak English.对不起,我不会说英语。 :) :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:在Codeigniter中的非对象上调用成员函数result() - Fatal error: Call to a member function result() on a non-object in Codeigniter 致命错误:在(CodeIgniter)中的非对象上调用成员函数query() - Fatal error: Call to a member function query() on a non-object in (CodeIgniter) codeigniter致命错误在非对象上调用成员函数cookie() - codeigniter fatal error Call to a member function cookie() on a non-object 致命错误:在非对象,代码点火器控制器上调用成员函数 - Fatal error: Call to a member function, on a non-object, codeigniter controller Codeigniter 致命错误:调用非对象上的成员函数 query() - Codeigniter Fatal error: Call to a member function query() on a non-object CodeIgniter随机PHP致命错误:在... mysqli_driver.php中的非对象上调用成员函数。 - CodeIgniter Random PHP Fatal error: Call to a member function … on a non-object in … mysqli_driver.php CodeIgniter致命错误:调用非对象上的成员函数display_clients() - CodeIgniter fatal error: Call to a member function display_clients() on a non-object Codeigniter / Postgresql:致命错误:在非对象上调用成员函数num_rows() - Codeigniter/Postgresql: Fatal error: Call to a member function num_rows() on a non-object Codeigniter-致命错误:在非对象上调用成员函数get_news() - Codeigniter- Fatal error: Call to a member function get_news() on a non-object 对SimpleXMLElement的getName()调用返回致命错误“在非对象上调用成员函数” - getName() call on SimpleXMLElement returns fatal error “call to member function on a non-object”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM