I wasted a few hours on Saturday morning struggling with the fact that CakePHP on my production server refused to save to a newly added field on a table in my database, while everything worked beautifully on my dev server. After hours of debugging on the live server I suddenly remembered that, with:
in core.php, as it should be on a production system, Cake uses the cached model definitions in tmp\cache\models and would therefore ignore any changes in the underlying database until these were refreshed. Simply either:
- Change temporarily to
Configure::write('debug', 2); and then run your code before changing it back again or;
- Delete the contents of tmp\cache\models
and it will pick up your new field.
This is annoying, but does not go against the HTTP specification, which states that GET requests are cacheable, whereas POST results are not. AJAX requests seem to generally default to GET (JQuery certainly does), so IE caches them.
Therefore, the solution is to make sure that AJAX requests use POST rather than GET. In jQuery, you can do this by setting:
For Prototype, you can set:
In the jQuery.ajax() method, there is also a ‘cache’ setting, which is true by default, but can be set to false, which prevents caching. It does this by appending a TIMESTAMP to the URL, so that each request is made to a different URL, so a cached result cannot be returned. If not using jQuery, an alternative to ensuring that your request is a POST request rather a GET request is to add a ‘cache-busting’ parameter, e.g. a timestamp, to the end of the request URL, e.g.:
var cacheBuster = new Date().getTime(); //Get timestamp
var url = 'http://www.example.com/getdata?cb=' + cacheBuster; //Add timestamp to URL