zen of coding

Modified something in your database? Be sure to clear the model cache!!!

This point deserves its own post.

Those who hang out on the friendly and helpful IRC channel, probably see this come up every week at least 3 times :)

Remembering to clear the cache every time the Data Base is modified, will hopefully save some people a few hours of head-scratching, because it solves a number of the problems…

What problems? … Well, let’s see:

– Bob modified some model, but the changes aren’t talking place (Did Bob modify the DB as well? Yep. Clear the cache!)
– Jerry renamed the file from user.php to user_table.php and renamed the class to “UserTable” (Modify DB? Clear the cache!)
– Somebody changed the primary key name, but the data in the view isn’t showing up (Yes, clear the cache!)

… this list can go on for a while, but the solution remains the same. Just remember to do it.

And thanks to room34 for pointing this problem out… and, actually, he quite amusingly describes this and another important issue of CakePHP on his blog. I really suggest everyone gives it a read as is it will solve some mysteries of CakePHP development.

  • 3 times a week? More like 3 times a day :p

  • If it does already – great, but if not, wouldn’t it be cool if the core schema update shell deleted the model cache files corresponding to tables that are updated.

    If its not possible due to file ownership permissions on the OS, at least a reminder could be displayed?

  • Pingback: Ayuda!CakePHP » Cambiando los modelos o las tablas()

  • @Mike

    Yeah, I guess that’s closer to the truth :)

    @Neil Crookes

    Not a bad idea, but then again is that an extra bloat for the core? … I mean life would be too easy if cake handled house keeping for us as well ;)

  • matt

    If you put your app in debug mode it in fact does clear the cache for you.

  • @matt

    Unfortunately it is not always the case, especially with older releases of cake. I haven’t tested extensively on the final, but this problem occurs too often for people not to be reminded of the remedy.

    If it does become a thing of the past with the latest releases, then we’ll all be just a little bit happier.

  • Well yeah, that sure was a problem I ran into! That’s why I love the `symfony cc` command!

  • @Evert Harmeling

    Well, hopefully it won’t be an issue at all in the 1.2 Final.

  • Pierre

    Same applies to translation strings when working with i18n.

    They are stored in app/tmp/cache/persistent/ and there is a file for each locale you use (ex.: cake_core_default_eng, cake_core_default_fre).

  • @Pierre

    Thanks for pointing this out. I’m actually working on an i18n’d project right now, and hopefully I’ll remember this once I run into this issue ;)

  • Just in case anybody else runs into this, I also wrote about a problem with a hosting service being caching overly aggressively causing weird problems that look like cache issues here: http://blog.accuweaver.com/2008/11/27/sourceforgenet-application-hosting-cache-issue/

    I had to add a ExpiresActive Off to my .htaccess files to fix the problem.

  • I’m currently dealing with a cache problem that makes my toenails curl (not in a good way) and my gray hairs to multiply (definitely not in a good way). I’ll check into the ExpiresActive property, hope it works.

  • So how do we exactly clear the mode cache? Perhaps I missed this.

  • @Kate C2

    You need to remove files (not directories) under app/tmp/cache/models.
    Also in the latest versions of cake cache gets rebuilt automatically if you are not in production (i.e. debug > 0)

  • Thanks. Big time saver and stopped me scratching my head.

%d bloggers like this: