Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP added column doesn't appear in query

I have added a new_column to a MyISAM table on a MySQLproduction server.

I have deleted the appropriate model cache file from the /app/tmp/models folder.

Upon reloading the page and inspection, the newly generated cache file for the model INCLUDES the new column.

The new column doesn't appear in SQL queries or results when read with fields => 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

Possible caveats and additional info:

  • The table has 39 columns (why would this be a problem?)
  • The model is an EXTENDED class from a plugin
  • The new column definition is TEXT DEFAULT null
  • Previous updates to the table gave expected results
  • Local development replica of the app doesn't have this issue
  • The test controller action enables Configure::write(debug => 2)
  • CakePHP 2.3.6, PHP 5.4.19, MySQL 5.1.70

How to make the Model recognize the new table definition? Are there any other cache systems I should investigate?


Edit #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'
)

Edit #2:

I have tried disabling the cache completely. Although the model picks up the new column, it is not really a solution.

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

Edit #3:

Further debugging shows that there is a discrepancy between these two results:

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

And this temporarily solves the problem:

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

Method caching of the DboSource is the source of this issue. Slowly getting to the bottom of things.. :D

like image 678
Vanja D. Avatar asked Oct 07 '13 22:10

Vanja D.


1 Answers

You have to clear all the cache files (but not directories) in app/tmp/cache whenever you update an application that is in production (debug is set to 0).

The actual cache causing problems in this issue was the app/tmp/cache/persistent/_cake_core_method_cache. Don't let the persistent part fool you, as it did me.

See this great answer for more details.

like image 192
Vanja D. Avatar answered Sep 30 '22 02:09

Vanja D.