Disable Cakephp's Auto Model "feature"

Go To StackoverFlow.com

2

In cake 1.2 there is a feature that allows the developer to no have to create models, but rather have cake do the detective work at run time and create the model for you. This process happens each time and is neat but in my case very hazardous. I read about this somewhere and now I'm experiencing the bad side of this.

I've created a plugin with all the files and everything appeared to be just great. That is until i tried to use some of the model's associations and functions. Then cake claims that this model i've created doesn't exist. I've narrowed it down to cake using this auto model feature instead of throwing and error! So i have no idea what's wrong!

Does anybody know how to disable this auto model feature? It's a good thought, but I can't seem to find where i've gone wrong with my plugin and an error would be very helpful!

2008-09-19 07:59
by SeanDowney


3

Cake 1.2

It's a hack and it's ugly cus you need to edit core cake files but this is how i do it: \cake\libs\class_registry.php : line 127ish

if (App::import($type, $plugin . $class)) {
    ${$class} =& new $class($options);
} elseif ($type === 'Model') {
    /* Print out whatever debug info we have then exit */
    pr($objects);
    die("unable to find class $type, $plugin$class");
    /* We don't want to base this on the app model */
    ${$class} =& new AppModel($options);
}

Cake 2

Costa recommends changing $strict to true in the init function on line 95 of Cake\Utility\ClassRegistry.php

See Cake Api Docs for init

ClassRegistry.php - init function

2008-09-20 03:36
by SeanDowney
In Cake 2, you edit Cake\Utility\ClassRegistry.php and change function init($class, $strict = false) to function init($class, $strict = true). It'll throw a Fatal Error: Call to a member function getDataSource() on a non-object if the model file doesn't exist - Costa 2013-02-02 04:35
Thanks, I'll update my answer to reflect thi - SeanDowney 2013-02-20 17:46


3

There's always the possibility to actually create the model file and set var $useTable = false.
If this is not what you're asking for and the model and its associations actually do exist, but Cake seems to be unable to find them, you'll have to triple check the names of all models and their class names in both the actual model definition and in the association definitions.

AFAIK you can't disable the auto modelling.

2008-09-19 08:12
by deceze


1

Use

var $useTable = false;

in your model definition.

2008-09-19 08:15
by Mladen Mihajlovic


1

Delete all cached files (all files under app/tmp, keep the folders)

In most cases where models seem to be acting in unexpected ways, often they dont include changes you've made, it is because that cake is useing an old cached version of the model.

2008-09-28 22:25
by Alexander Morland


0

Uh...where do we start. First, as Alexander suggested, clear your app cache.

If you still get the same behaviour, there is probably something wrong with the class and/or file names.

Remember the rules, for controller:

* classname: BlastsController
* filename: blasts_controller.php

for model:

* classname: Blast
* filename: blast.php

Don't foget to handle the irregular inflections properly.

2009-04-18 20:20
by dr Hannibal Lecter
Ads