2014-01-26 38 views
3

我StatsController.php與CakePHP的:與模型和表名問題

class StatsController extends AppController { 
    public function edit($id = null) { 
     var_dump($this->Stat); 
    } 
} 

我StatModel.php與

class Stat extends AppModel { 
    public $useTable = 'tips'; 
    // .... 
} 

當我嘗試保存我的數據,我最初的錯誤是:

Missing Database Table | Error: Table stats for model Stat was not found in datasource default.

我在做什麼錯,謝謝。

這裏是的var_dump:

object(AppModel)[26] 
    public 'useDbConfig' => string 'default' (length=7) 
    public 'useTable' => string 'stats' (length=5) 
    public 'id' => null 
    public 'data' => 
    array (size=0) 
     empty 
    public 'schemaName' => null 
    public 'table' => string 'stats' (length=5) 
    public 'primaryKey' => string 'id' (length=2) 
    protected '_schema' => null 
    public 'validate' => 
    array (size=0) 
     empty 
    public 'validationErrors' => 
    array (size=0) 
     empty 
    public 'validationDomain' => null 
    public 'plugin' => null 
    public 'name' => string 'Stat' (length=4) 
    public 'alias' => string 'Stat' (length=4) 
    public 'tableToModel' => 
    array (size=1) 
     'stats' => string 'Stat' (length=4) 
    public 'cacheQueries' => boolean false 
    public 'belongsTo' => 
    array (size=0) 
     empty 
    public 'hasOne' => 
    array (size=0) 
     empty 
    public 'hasMany' => 
    array (size=0) 
     empty 
    public 'hasAndBelongsToMany' => 
    array (size=0) 
     empty 
    public 'actsAs' => null 
    public 'Behaviors' => 
    object(BehaviorCollection)[27] 
     public 'modelName' => string 'Stat' (length=4) 
     protected '_methods' => 
     array (size=0) 
      empty 
     protected '_mappedMethods' => 
     array (size=0) 
      empty 
     protected '_enabled' => 
     array (size=0) 
      empty 
     protected '_loaded' => 
     array (size=0) 
      empty 
     public 'defaultPriority' => int 10 
    public 'whitelist' => 
    array (size=0) 
     empty 
    public 'cacheSources' => boolean true 
    public 'findQueryType' => null 
    public 'recursive' => int 1 
    public 'order' => null 
    public 'virtualFields' => 
    array (size=0) 
     empty 
    protected '_associationKeys' => 
    array (size=4) 
     'belongsTo' => 
     array (size=6) 
      0 => string 'className' (length=9) 
      1 => string 'foreignKey' (length=10) 
      2 => string 'conditions' (length=10) 
      3 => string 'fields' (length=6) 
      4 => string 'order' (length=5) 
      5 => string 'counterCache' (length=12) 
     'hasOne' => 
     array (size=6) 
      0 => string 'className' (length=9) 
      1 => string 'foreignKey' (length=10) 
      2 => string 'conditions' (length=10) 
      3 => string 'fields' (length=6) 
      4 => string 'order' (length=5) 
      5 => string 'dependent' (length=9) 
     'hasMany' => 
     array (size=11) 
      0 => string 'className' (length=9) 
      1 => string 'foreignKey' (length=10) 
      2 => string 'conditions' (length=10) 
      3 => string 'fields' (length=6) 
      4 => string 'order' (length=5) 
      5 => string 'limit' (length=5) 
      6 => string 'offset' (length=6) 
      7 => string 'dependent' (length=9) 
      8 => string 'exclusive' (length=9) 
      9 => string 'finderQuery' (length=11) 
      10 => string 'counterQuery' (length=12) 
     'hasAndBelongsToMany' => 
     array (size=12) 
      0 => string 'className' (length=9) 
      1 => string 'joinTable' (length=9) 
      2 => string 'with' (length=4) 
      3 => string 'foreignKey' (length=10) 
      4 => string 'associationForeignKey' (length=21) 
      5 => string 'conditions' (length=10) 
      6 => string 'fields' (length=6) 
      7 => string 'order' (length=5) 
      8 => string 'limit' (length=5) 
      9 => string 'offset' (length=6) 
      10 => string 'unique' (length=6) 
      11 => string 'finderQuery' (length=11) 
    protected '_associations' => 
    array (size=4) 
     0 => string 'belongsTo' (length=9) 
     1 => string 'hasOne' (length=6) 
     2 => string 'hasMany' (length=7) 
     3 => string 'hasAndBelongsToMany' (length=19) 
    public '__backAssociation' => 
    array (size=0) 
     empty 
    public '__backInnerAssociation' => 
    array (size=0) 
     empty 
    public '__backOriginalAssociation' => 
    array (size=0) 
     empty 
    public '__backContainableAssociation' => 
    array (size=0) 
     empty 
    protected '_insertID' => null 
    protected '_sourceConfigured' => boolean false 
    public 'findMethods' => 
    array (size=6) 
     'all' => boolean true 
     'first' => boolean true 
     'count' => boolean true 
     'neighbors' => boolean true 
     'list' => boolean true 
     'threaded' => boolean true 
    protected '_eventManager' => null 
    protected '_validator' => null 
+0

請澄清:「看來我的模型沒有使用」。您在「編輯」操作中實際嘗試的是什麼,哪些不起作用?另外,你在什麼版本的CakePHP中?而且,你爲什麼在你的模型中有「blablallalsaddasdasdasd」。 – Dave

+0

@Dave此時在我的編輯功能,我只有這個代碼,很好。當我說'我的模型未被使用'時,我的意思是它似乎沒有被加載,因爲我沒有PHP錯誤(在我的代碼中,我放錯了東西來產生錯誤)。如果一切正常,它應該打破代碼,但事實並非如此。我希望它更清楚。使用的版本是2.4.4 – zeflex

+0

不要問一個問題不存在,請嘗試給我們實際的問題。就像...當我嘗試執行find()時,它提供了一個錯誤,或者返回了錯誤的數據......等等。你基本上說「這裏有一些沒有問題的代碼 - 問題是什麼?」答案 - 沒有。 – Dave

回答

4

I have StatModel.php with

文件StatModel.php重命名爲Stat.php,並確保它位於您Model文件夾中。模型文件不附加單詞Model。我知道,這是與控制器的不一致,但我沒寫蛋糕:)

When I call /stats/edit, it seems my model is not used, I didn't get any errors and also I am not able to change the table name (I want to use tips table)

CakePHP中找不到Stat.php文件在你的模型文件夾,這樣它會自動創建一個假的模型。使用模型名稱作爲模式,它會查找stats表。

+0

太棒了!感謝馬修! – zeflex

+0

我犯了很多次這個錯誤。我希望有一個功能可以在Cake中禁用自動模型。等到你必須追查失敗的關聯。 – cgTag

+0

Fake - > AppModel實例。這可以通過調試(get_class($ this-> Stat))來確認;' – AD7six

-2

我猜偏轉不能解決統計正確統計。不要使用縮寫,爲什麼不統計?爲什麼不使用TipsController來處理提示表?

但是,如果你想使用這種模式的嘗試:

public $uses = array('Stat'); 

在控制器中明確設置這一模式。

+0

它已經加載模型,或者它不會成功調試'$ this-> Stat',對吧? – Dave

+0

我沒有嘗試,但其他答案解決了我的問題。謝謝btw。 – zeflex

+0

-1這裏沒有任何錯誤,或使用縮寫。 – AD7six