【问题标题】:Kohana ORM: Array to string conversion ErrorKohana ORM:数组到字符串转换错误
【发布时间】:2012-01-10 19:41:29
【问题描述】:

我尝试在本地 xampp 上使用 Kohana/ORM,但出现以下错误

ErrorException [注意]:数组到字符串的转换 MODPATH\orm\classes\kohana\orm.php [980]

975             }
976             else
977             {
978                 // List columns and mirror for performance
979                 $this->_table_columns = $this->list_columns();
980                 $this->_table_columns = array_combine($this->_table_columns, $this->_table_columns);
981 
982                 // Load column cache
983                 ORM::$_column_cache[$this->_object_name] = $this->_table_columns;
984             }
985         }

这似乎是不同框架/PHP应用程序中出现的常见错误,但我没有找到任何修复它的线索。

模型只是基本的 ORM

class Model_Product extends ORM {

}

Mysql 表 (InnoDB - UTF-8) 有两个字段 id - 主整数 名称 - varchar 50

任何地方都没有巫毒,非常感谢您的帮助

提前致谢!

编辑:请求的 vardump

array(2) {
  ["id"]=>
  array(13) {
    ["type"]=>
    string(3) "int"
    ["min"]=>
    string(11) "-2147483648"
    ["max"]=>
    string(10) "2147483647"
    ["column_name"]=>
    string(2) "id"
    ["column_default"]=>
    NULL
    ["data_type"]=>
    string(3) "int"
    ["is_nullable"]=>
    bool(false)
    ["ordinal_position"]=>
    int(1)
    ["display"]=>
    string(2) "11"
    ["comment"]=>
    string(0) ""
    ["extra"]=>
    string(14) "auto_increment"
    ["key"]=>
    string(3) "PRI"
    ["privileges"]=>
    string(31) "select,insert,update,references"
  }
  ["name"]=>
  array(12) {
    ["type"]=>
    string(6) "string"
    ["column_name"]=>
    string(4) "name"
    ["column_default"]=>
    NULL
    ["data_type"]=>
    string(7) "varchar"
    ["is_nullable"]=>
    bool(false)
    ["ordinal_position"]=>
    int(2)
    ["character_maximum_length"]=>
    string(2) "50"
    ["collation_name"]=>
    string(15) "utf8_general_ci"
    ["comment"]=>
    string(0) ""
    ["extra"]=>
    string(0) ""
    ["key"]=>
    string(0) ""
    ["privileges"]=>
    string(31) "select,insert,update,references"
  }
}

【问题讨论】:

  • 请在您的问题中添加var_dump($this->list_columns())
  • 你用的是什么版本?
  • 最新的直接来自github,将kohana和orm都添加为子模块
  • @rootman,你有什么收获吗?有人有一个similar problem,只是想知道它是否/曾经是一个 PHP 错误?我看到您的 Kohana 错误报告因无效而关闭。

标签: php orm kohana


【解决方案1】:

第 980 行:

980                 $this->_table_columns = array_combine($this->_table_columns, $this->_table_columns);

考虑到前一行 979 看起来是多余的:

979                 $this->_table_columns = $this->list_columns();

合并两次同一个数组是没有用的,尤其是数组是这样的:

array(2) {
  ["id"]=>
  array(13) {
    ["type"]=>
    string(3) "int"
    ["min"]=>
    string(11) "-2147483648"
    ["max"]=>
    string(10) "2147483647"
    ["column_name"]=>
    string(2) "id"
    ["column_default"]=>
    NULL
    ["data_type"]=>
    string(3) "int"
    ["is_nullable"]=>
    bool(false)
    ["ordinal_position"]=>
    int(1)
    ["display"]=>
    string(2) "11"
    ["comment"]=>
    string(0) ""
    ["extra"]=>
    string(14) "auto_increment"
    ["key"]=>
    string(3) "PRI"
    ["privileges"]=>
    string(31) "select,insert,update,references"
  }
  ["name"]=>
  array(12) {
    ["type"]=>
    string(6) "string"
    ["column_name"]=>
    string(4) "name"
    ["column_default"]=>
    NULL
    ["data_type"]=>
    string(7) "varchar"
    ["is_nullable"]=>
    bool(false)
    ["ordinal_position"]=>
    int(2)
    ["character_maximum_length"]=>
    string(2) "50"
    ["collation_name"]=>
    string(15) "utf8_general_ci"
    ["comment"]=>
    string(0) ""
    ["extra"]=>
    string(0) ""
    ["key"]=>
    string(0) ""
    ["privileges"]=>
    string(31) "select,insert,update,references"
  }
}

它只包含字符串键。您应该使用 kohana 框架打开错误报告。

评论第 980 行,直到修复此问题。

【讨论】:

  • 你是对的,这条线只是......没用,但是这样做时会弹出第二个错误,同样的错误,但这次是在第 931 行: $values = array_combine($this- >_table_columns, array_fill(0, count($this->_table_columns), NULL));我想我会向 kohana 报告这个问题,看看他们能做什么
  • 这种看起来多余的线条的问题在于它们包含一些考古信息,并且 - 如您所见 - 隐藏缺陷。根据您的描述,$this->_table_columns 在某些情况下不包含数组。正如预期的那样,我几乎不会在第 980 行 if (!is_array($this->_table_columns)) throw new UnexpectedValueException('Not an array'); 中退出 - 然后您将收到致命错误而不是通知,但它会阻止应用程序处于未定义状态。它也可能有助于通过回溯找到导致这种情况的原因。
  • 我在 dev.kohanaframework.org/issues/4352 创建了一个问题。我认为这是一个应该尽快解决的问题,因为如果您不想修改核心文件,它会阻止进一步的开发。
  • 你可以看看你的代码的哪一部分触发了它。这可能是 Kohana 代码库中的一个缺陷,但它的原因可能在于您的代码。我不会认为这很紧急。我的意思是紧急真的很紧急,没有必要围绕与输入验证相关的缺陷进行炒作。此外,如果您能真正找到问题的原因,那就太好了。这有助于最终修复错误。
  • 好吧,我只是通过 ORM::factory('product') 调用模型,它破坏了一切。老实说,我之前从未使用过 Bugtracker,所以紧急可能是错误的选择,我只是认为这是一个重要的错误。不过我会尝试找出原因并尝试提供解决方法
【解决方案2】:

我找到了解决此错误的方法,只需在模型中声明表列即可。

protected $_table_columns = array(
    'column' => NULL,
    'names'  => NULL,
    'go'     => NULL,
    'here'   => NULL,
    ......
);

这将解决问题并提高性能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-14
    • 2015-02-26
    • 2012-07-15
    • 2017-12-02
    • 2013-06-23
    • 2013-08-24
    • 1970-01-01
    相关资源
    最近更新 更多