CakePHP schema shell gotcha

Posted in CakePHP on 16.06.2012.

Ever had one of those "cannot load your schema.php" even though it's right there? Here's why...

Most of the time, I go on a killing spree when I see people not reading what is written on their screen. Quite often, the solution to a problem is in the error message, and people are just too lazy to read it properly, which leads to bad questions.

However, this one time, on camping, I didn't go on a killing spree, because I'd be killing myself. And I hate when that happens. Why would I be killing myself? I'm glad you asked!

I've never actually paid much attention to some details of the cake schema shell, and it is a possibility that I still don't. Still, one thing bugged me a few times, it may be familiar to some of you:

$ cake schema create
.../Config/Schema/schema.php could not be loaded

But this works of course:

$ cake schema view
# gives schema file content

So what's the deal? Why the hell would view work but create not?

Turns out to be quite simple: Because your schema has a different name from your application. If that doesn't make any sense, don't worry, it's okay.

Here's how it works:

When you generate your schema, cake takes your application folder (usually just app) and creates an AppSchema class. Later, when you unload your application into a my-arse-kicking-application folder, cake will still know it needs to read the schema.php file, and that's all fine and dandy. What it will not do however, is look for an AppSchema class. It will look for something like MyArseKickingApplicationSchema. And when it can't find it, it gives you the good ol' Config/Schema/schema.php could not be loaded.

Why? Beats me. I can obviously see how this makes sense for plugins, you don't want duplicate classes laying around, that would be a recipe for clusterintercourse (I don't swear). But I don't understand why this naming scheme is used for the main app. There should be only one AppSchema, right? No? Okay...

It doesn't matter, I'm assuming there is a reason. I worked around the problem by renaming my app folder to my old name, which is actually a bad workaround. Today I discovered, with a simple inspection of the schema shell code, that you can actually tell the schema shell what's the name of your class:

$ cake schema create MyArseKickingApplication


$ cake schema create App

...whatever your case may be.

Simple? Yes! Obvious on first sight? Not so much. In any case, I have to finish my killing spree and writing this is causing massive delays.

Happy baking!

Article comments — View · Add

No comments!