4

I've recently added a new column to my 'users' table (first_name), however when I use

$this->User->find('all')

it will not return first_name in the array.

However, if I use

$this->User->find('all', array('fields' => 'first_id'))

It returns fine.

I have cleared the cache in tmp/cache/model but the problem still persists

Any ideas why I'm having this problem?

sharif9876
  • 680
  • 6
  • 19
  • +1 - I have had the same problem and hope someone can provide a good answer. Until you get a good reason, I overcome this problem setting debug to 2 (in core.php), then request pages based the changed models. Then setting it back to zero. I am not sure what else it does besides rebuilding the cache files, but this clears the problem for me. – AgRizzo May 11 '14 at 14:31
  • 4
    This must be a duplicate though I can't find the canonical answer - the simple solution is to clear **all files** in the tmp/cache dir. You're not currently doing that. – AD7six May 11 '14 at 15:21
  • @AD7six Yes clearing all the files did work thanks :), so what data is stored in the persistent cache then? – sharif9876 May 11 '14 at 16:01
  • 2
    [Solution and explanation](http://stackoverflow.com/a/3118292/216084) –  Jun 04 '14 at 08:29
  • When you make any changes to database like adding new columns to table require debug mode 2 to affect that changes. Make debug mode 2, Load page which uses your module "User" where u are selecting or updating something to users table and then see if you get column 'first_name' – Yashrajsinh Jadeja Jun 04 '14 at 08:30
  • 2
    possible duplicate of [cakephp - why do some changes NOT happen until I switch debugging to 3?](http://stackoverflow.com/questions/3111720/cakephp-why-do-some-changes-not-happen-until-i-switch-debugging-to-3) – jeremyharris Jun 05 '14 at 21:27
  • you can go to your core.php file and try to change your current debug value. Configure::write('debug', 1); //in this line try to change to any numbers. Then save it. refresh your site. Then check if your site can now read your updates in DB. then you can reset your debug value to the appropriate one. This is the method I've been using. It seems that if you don't refresh your current debug value, the models are using its old records for faster processing (caching). So by changing its debug value, its like refreshing the cache :) – decodingpanda Jul 04 '14 at 05:51

2 Answers2

1

You'll want to refresh your model cache in the tmp/cache folder.

Optionally, what I have found useful for my development environment is to have a utility function available to run each time I make changes to the model/db layer. In an admin controller I have a clearCache function:

App::uses('Folder', 'Utility');

App::uses('File', 'Utility');

public function clearCache(){

        Configure::write('debug', 2);

        Cache::clear();
        $msg = '';

        $models      = new Folder('../tmp/cache/models');
        if(!$models->delete())
            $msg .= 'Could not delete models folder!<br>';
        else
            $msg .= 'Models folder deleted.<br>';

        $models = new Folder();
        if($models->create('../tmp/cache/models'))
            $msg .= 'New models folder created.<br>';
        else 
            $msg .= 'Could not create new models folder!<br>';

        $persistent  = new Folder('../tmp/cache/persistent');
        if(!$persistent->delete())
            $msg .= 'Could not delete persistent folder!<br>';
        else
            $msg .= 'Persistent folder deleted.<br>';

        $persistent = new Folder();
        if($persistent->create('../tmp/cache/persistent'))
            $msg .= 'New models folder created.<br>';
        else 
            $msg .= 'Could not create new persistent folder!<br>';

        $views       = new Folder('../tmp/cache/views');
        if(!$views->delete())
            $msg .= 'Could not delete views folder!<br>';
        else
            $msg .= 'Views folder deleted.<br>';

        $views = new Folder();
        if($views->create('../tmp/cache/views'))
            $msg .= 'New views folder created.<br>';
        else 
            $msg .= 'Could not create new views folder!<br>';

        $this->set('results', $msg);
}
jRoB
  • 338
  • 1
  • 9
1

It happens whenever you update or edit your database.You simply have to do is change the debug mode if it is set to 0 then put it 2 and vise versa. Your problem will be solved.

chirag
  • 523
  • 7
  • 13