English 中文(简体)
Prevent controller from trying to autoload model
原标题:
  • 时间:2009-11-08 19:14:14
  •  标签:
  • php
  • cakephp

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 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





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签