【问题标题】:Missing Database Table cakephp even table is there缺少数据库表 cakephp 甚至表在那里
【发布时间】:2014-07-21 13:51:04
【问题描述】:

我有一个数据库,其中所有表都以 "bs" 为前缀。我从 bs_states 表中烘焙了一个模型,它生成了以下代码。

<?php
App::uses('AppModel', 'Model');
/**
 * State Model
 *
 * @property country $Country
 * @property Seller $Seller
 */
class State extends AppModel {

/**
 * Use table
 *
 * @var mixed False or table name
 */
    public $useTable = '_states';

/**
 * Primary key field
 *
 * @var string
 */
    public $primaryKey = 'state_id';

/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'state_iso';


    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Country' => array(
            'className' => 'country',
            'foreignKey' => 'country_iso',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Seller' => array(
            'className' => 'Seller',
            'foreignKey' => 'state_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

此代码在本地系统中运行良好,但是当我将其上传到生产服务器时,它显示以下消息。

缺少数据库表 错误:在数据源默认值中找不到模型状态的表 bsstates。

最初我认为这是一个缓存问题,所以我做了以下操作:

  • 我已经清除了 tmp/cache 目录
  • 我将调试模式Configure::write('debug', 0);改为Configure::write('debug', 2);

但没有运气。我仍然面临同样的问题。我在生产服务器上的数据库连接很好,因为其他模型工作正常。

【问题讨论】:

  • 你确定吗?您的代码说它应该在bsstates 之间有一个下划线:public $useTable = '_states';
  • 也粘贴你的数据库文件(显然不包括用户名/密码)
  • 数据库中的表名是什么?
  • @Andy 是的,它在我的本地系统中运行良好。
  • 而不是 public $useTable = 'states';你可以写 public $useTable = 'states';并在 database.php 中的数据库配置的前缀中添加下划线()作为“bs_”。

标签: php cakephp cakephp-2.0 cakephp-2.3 cakephp-model


【解决方案1】:

删除 $useTable 属性并使用

public $tablePrefix = 'bs_';

【讨论】:

  • 你收到了什么信息?
  • 还是同样的信息。缺少数据库表错误:在数据源默认值中找不到模型状态的表 bsstates。
  • 好吧,如果这是消息,那么使用'bs'而不是'bs_'
【解决方案2】:

在生产环境中首先要查看的是日志。它们还提供了比调试打印输出更多的信息。导航到这里:

\app\tmp\logs

这个错误通常伴随着对表的权限不足。确保 Cake 的 db 角色对该表具有特权。

既然你说它在本地工作得很好,这让我觉得这是一个数据库权限问题,而且 CakePHP 约定似乎也被取消了。

模型类名应该是:

BsStates

类变量$useTable应该是:

public $useTable = 'bs_states';

【讨论】:

    【解决方案3】:

    而不是写public $useTable = '_states'; 你可以写信和public $useTable = 'states'; 添加下划线() 在 database.php 中的数据库配置中的前缀为 'bs_'。

    或在此文件中添加public $tablePrefix = 'bs_';

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 2013-02-09
      • 1970-01-01
      • 2013-07-14
      • 2019-03-26
      • 1970-01-01
      • 2017-05-04
      相关资源
      最近更新 更多