mysql - CakePHP 添加的列没有出现在查询中

标签 mysql cakephp caching model

我已将 new_column 添加到 MySQL 生产服务器上的 MyISAM 表中。

我已经从/app/tmp/models 文件夹中删除了相应的模型缓存文件。

重新加载页面并检查后,为模型新生成的缓存文件包含新列

新列不会出现在 SQL 查询中,或者在使用字段 => null 读取时出现。

debug($this->read(null, $id));
  //Query generates fields: SELECT Model.column_a, Model.column_b, Model.id WHERE ...
  //Results do not include the new column

debug($this->read(array('new_column'), $id));
  //Query generates fields: SELECT Model.new_column, Model.id WHERE ...
  //Results include the new column and id

可能的注意事项和附加信息:

  • 该表有 39 列(为什么这是个问题?)
  • 该模型是来自插件的扩展类
  • 新列定义为 TEXT DEFAULT null
  • 之前对表格的更新给出了预期的结果
  • 应用的本地开发副本没有这个问题
  • 测试 Controller 操作启用 Configure::write(debug => 2)
  • CakePHP 2.3.6、PHP 5.4.19、MySQL 5.1.70

如何让模型识别新的表定义?还有其他我应该研究的缓存系统吗?


编辑#1:

debug($this->ModelName->getDataSource()->describe('table_name'));
// outputs all columns
// ...
'new_column' => array(
    'type' => 'text',
    'null' => true,
    'default' => null,
    'length' => null,
    'collate' => 'utf8_general_ci',
    'charset' => 'utf8'
)

编辑#2:

我试过完全禁用缓存。虽然模型选择了新列,但它并不是真正的解决方案。

Configure::write('Cache.disable', true);
// new_column appears in both query and results

编辑#3:

进一步调试发现这两个结果存在差异:

$this->ModelName->getDataSource()->fields($this->Model, 'ModelName');
//returns fields without new_colum
array_keys($this->ModelName->schema());
//returns fields with new_column

这暂时解决了问题:

$ds = $this->ModelName->getDataSource();
$ds->cacheMethods = false;
...

DboSource 的方法缓存是此问题的根源。慢慢追根究底.. :D

最佳答案

每当您更新生产中的应用程序(调试设置为 0)时,您必须清除 app/tmp/cache 中的所有缓存文件(但不是目录)。

导致此问题的实际缓存是 app/tmp/cache/persistent/_cake_core_method_cache。不要让持久部分欺骗了你,就像我一样。

参见 this great answer了解更多详情。

关于mysql - CakePHP 添加的列没有出现在查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19236019/

相关文章:

java - 如何解决org.hibernate.hql.ast.QuerySyntaxException : Exception

cakephp - 将 Sqlite3 与 CakePHP 结合使用

php - CakePHP: "GET"表单在提交后不自动填充表单字段

spring-boot - Spring Boot native 缓存 : How to expire/remove cache data by individual keys/elements

mysql - 获取mysql中除今天记录外的当月记录

php - MySQL 服务器错误 -> 服务器消失并发送查询数据包时出错

php - 检查电子邮件是否存在于 MySQL 数据库中

php - CakePHP 多个 HABTM 关系

c# - 线程安全缓存机制(不是.NET内置缓存) ASPX C#

ruby-on-rails - rails : Skip controller if cache fragment exist (with cache_key)