开发者

Prevent controller from trying to autoload model

开发者 https://www.devze.com 2022-12-10 16:37 出处:网络
I am a beginning Cake user and trying to do some work on an already existing application. Running into a problem when I create a new controller. I have created StoreController and when I try开发者_JAV

I am a beginning Cake user and trying to do some work on an already existing application. Running into a problem when I create a new controller. I have created StoreController and when I try开发者_JAVA百科 to call methods inside it I get the error below. There is no table 'stores', but it seems like it's trying to automatically load a model relating to the controller. How can I prevent my application from trying to load a model for this controller?

Missing Database Table
Error: Database table stores for model Store was not found.


That will do it, you can also just assign it to an empty array like so

var $uses = array();


Think I found it...

class StoreController extends AppController {

// Do not preload any models, we will do this on demand to prevent waste.
var $uses = '';


If you have an AppController like so..

<?php
    Class AppController extends Controller {
        public $uses = array( 'GlobalModel' );
    }
?>

And you use an empty array...

<?php
    Class StoresController extends AppController {
        public $uses = array( );
    }
?>

Then the StoresController will still have access to the GlobalModel from the AppController

If you use

<?php
    Class StoresController extends AppController {
        public $uses = '';
    }
?>

Then the StoresController won't have access to ANY models.

A good amount of the time when someone wants a controller without a model, it is because they don't want to associate the controller with a database table. But for the purposes of making validation easier on submitted data etc what you might want to consider is

<?php
    Class StoresController extends AppModel {
        public $name = "Stores";
        public $uses = array( 'Store' );
    }
?>

<?php
    Class Store extends AppModel {
        public $name = "Store";
        public $useTable = false;
    }
?>

Then you can use the Model::_schema property. That is more than you asked for though, so I will let you do your own research on _schema and validating data that won't be handled by a DB table.

http://book.cakephp.org/view/442/_schema

0

精彩评论

暂无评论...
验证码 换一张
取 消