7

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

Vanja D.
  • 834
  • 13
  • 20

3 Answers3

10

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.

Community
  • 1
  • 1
Vanja D.
  • 834
  • 13
  • 20
  • How did the _persistent_ part fool you (there's no other reference to persistent in the question/answers)? Generally speaking, clearing the cache means deleting all the files under `tmp`. – AD7six Oct 08 '13 at 09:30
  • I do not clear the cache so often, so I was cautious not to break any existing functionality. "Persistent" is a word that translates to "should stay where it is" in my brain. :) I also guessed that issue resides in the model, so _why clear anything else_? [the book of Caching](http://book.cakephp.org/2.0/en/core-libraries/caching.html) might include a section about clearing. – Vanja D. Oct 09 '13 at 14:13
  • 1
    You're my hero of the day .. I was tired of deleting models from the cache and seeing no luck .. I didn't know I have to delete files under persistent too – Asif Ali Oct 05 '19 at 07:49
0

It is probably something related to the Database cache engine: in this case you can solve your issue just changing the query (for example, removing a field using the 'fields' statement). You can even try deleting the CakePHP's model cache: it's located in app/tmp/cache/models.

Happy Coding!

Andrea Alhena
  • 986
  • 6
  • 9
  • I have deleted all files from app/tmp/cache/models, to no avail. But it is certainly a caching problem, see edit #2 – Vanja D. Oct 07 '13 at 23:04
  • As I said, have you tried changing the query adding or removing a field? This will explain if it's a query caching issue or not. – Andrea Alhena Oct 07 '13 at 23:09
  • Yes, I have. I have also disabled/enabled Model->cacheQueries and tried with 'cache' query parameter. No success. The only thing that helped so far was disabling the caching at the core. – Vanja D. Oct 07 '13 at 23:41
0

In fact what you can add a bootstrap file if you use a Plugin. In my global bootstrap file I put:

CakePlugin::load('Myplugin',    array('routes' => true, 'bootstrap' => true));

and in Plugin/Myplugin/Config/bootstrap.php I have

<?php
Cache::config('_cake_core_', array(
    'engine' => 'File',
    'prefix' => 'cake_core_myplugin_',
    'path' => CACHE . 'persistent' . DS,
    'serialize' => true,
    'duration' => '+7 days',
));
?>

I added two columns in one of my models and I faced the same issue has you. Then I deleted all the models and persistent cache files and I change the duration inside my bootstrap file to 1 min. Finally, my model was ok and reloaded. Later I go back to the initial duration. It is durty, but it also looks like CakePHP messed up a little bit with cache.

Hope that it can help

nourcy
  • 101
  • 1