2016-04-20 96 views
1

我剛剛用CodeIgniter設置了DataMapper並根據需要編寫了模型。CodeIgniter DataMapper表名覆蓋不起作用

但是我似乎錯過了一些東西,因爲當我運行查詢時,DataMapper無法看到表名覆蓋並認爲根本沒有表。

有趣的是,如果我刪除覆蓋,它的工作原理應該如此(雖然有一個錯誤說自動生成的表不存在)。

型號代碼:

class Activity extends Datamapper { 

var $table = 'activity_log'; 

var $has_one = [ 
    'user' 
]; 

} 

控制器代碼:

$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 

正如我上面提到的,如果刪除變量$表= 'activity_log';那麼它就會推遲去尋找表格「活動」。

回答

0

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

+0

不幸的是,這並沒有解決任何問題。即使模型名稱爲'activity_log'且模型被更改爲'Activity_log',它仍然失敗。我知道datamapper通常如何引用數據庫,但我也意識到重寫此行爲(使用'var $ table ='activity_log')'。我的問題是,當存在覆蓋時,在查詢發生之前,該表被設置爲「空」。 – Stonestorm

+0

'var'是不推薦使用的關鍵字。嘗試使用「公共」(可見性)關鍵字。 – Tpojka

+0

沒什麼變化,我很害怕。 – Stonestorm