php - CodeIgniter DataMapper 表名覆盖不起作用

标签 php mysql codeigniter datamapper

我刚刚使用 CodeIgniter 设置了 DataMapper 并根据需要编写了模型。

但是我似乎遗漏了一些东西,因为当我运行查询时,DataMapper 看不到表名覆盖并认为根本没有表。

有趣的是,如果我删除覆盖,它会正常工作(尽管有一个错误说自动生成的表格不存在)。

模型代码:

class Activity extends Datamapper {

var $table = 'activity_log';

var $has_one = [
    'user'
]; 

}

Controller 代码:

$activity = new Activity(1);

错误:

Error Number: 1064 
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right
syntax to use near 'WHERE `.`id` = 1' at line 2

SELECT * WHERE `.`id` = 1

正如我上面提到的,如果我删除 var $tables = 'activity_log';然后它会按原样推迟回寻找表“事件”。

最佳答案

Rules

DataMapper models must be named the singular version of the object name, with an uppercase first letter. So for a user object, the DataMapper model would be named User. The model should have a corresponding table in the database named as the lowercase, pluralised version of the object name. So for a DataMapper model named User, the table would be named users. For a DataMapper model named Country, the table would be named countries.

In most cases, the difference between the singular and plural version of an object name is just a matter of adding the letter s on the end.

您的模型应命名为 Activity_log

Docs .

关于php - CodeIgniter DataMapper 表名覆盖不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36756201/

相关文章:

php - Codeigniter - 使用事件记录时处理错误

php - 阻止 SELECT 直到结果可用

php - Sphinx 和 PHP 的一些问题

php - 对于 PDO PHP,将值存储在 var 中安全吗?

php - 用于查找和替换的 SQL 查询不起作用

php - Codeigniter 和 oracle 未定义常量 : OCI_COMMIT_ON_SUCCESS

javascript - PHP - window.onbeforeunload 和自动注销冲突

php - getElementId 返回 null 或 undefined

mysql - 数据库的自动时间戳新条目(phpMyAdmin)

mysql - SQL 中的累积和