3

我有 StatsController.php

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

我有 StatModel.php

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

当我尝试保存数据时,我最初的错误是:

缺少数据库表 | 错误:在数据源默认值中找不到模型 Stat 的表统计信息。

我做错了什么,谢谢。

这是 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
4

2 回答 2

4

我有 StatModel.php

将文件重命名StatModel.phpStat.php并确保它位于您的Model文件夹中。模型文件不附加单词Model. 我知道,它与控制器不一致,但我没有写 Cake :)

当我调用 /stats/edit 时,我的模型似乎没有被使用,我没有收到任何错误,而且我无法更改表名(我想使用提示表)

CakePHP 在您的模型文件夹中找不到该Stat.php文件,因此它会自动创建一个假模型。使用模型名称作为模式,它会查找stats表格。

于 2014-01-26T19:27:34.970 回答
-2

我猜变形器无法将 Stats 正确解析为 Stat。不要使用缩写,为什么不使用统计数据?如果您处理提示表,为什么根本不使用 TipsController?

但是,如果您想使用该模型,请尝试:

public $uses = array('Stat');

在您的控制器中明确设置该模型。

于 2014-01-26T19:22:23.077 回答