2011/09/16

CakePHP でモデルを使わないコントローラーにて Notice が大量発生する

どべーっと。
大量に出ることがあります。
ちょっとした間違いが原因でした。

CakePHP は 1.3.10


Notice (8): Constant SECOND already defined [CORE/cake/basics.php, line 26]
Notice (8): Constant MINUTE already defined [CORE/cake/basics.php, line 27]
Notice (8): Constant HOUR already defined [CORE/cake/basics.php, line 28]
Notice (8): Constant DAY already defined [CORE/cake/basics.php, line 29]
Notice (8): Constant WEEK already defined [CORE/cake/basics.php, line 30]
Notice (8): Constant MONTH already defined [CORE/cake/basics.php, line 31]
Notice (8): Constant YEAR already defined [CORE/cake/basics.php, line 32]
Notice (8): Constant CAKE already defined [CORE/cake/config/paths.php, line 39]
Notice (8): Constant MODELS already defined [CORE/cake/config/paths.php, line 51]
Notice (8): Constant BEHAVIORS already defined [CORE/cake/config/paths.php, line 56]
Notice (8): Constant CONTROLLERS already defined [CORE/cake/config/paths.php, line 61]
Notice (8): Constant COMPONENTS already defined [CORE/cake/config/paths.php, line 66]
Notice (8): Constant APPLIBS already defined [CORE/cake/config/paths.php, line 71]
Notice (8): Constant VIEWS already defined [CORE/cake/config/paths.php, line 76]
Notice (8): Constant HELPERS already defined [CORE/cake/config/paths.php, line 81]
Notice (8): Constant LAYOUTS already defined [CORE/cake/config/paths.php, line 86]
Notice (8): Constant ELEMENTS already defined [CORE/cake/config/paths.php, line 93]
Notice (8): Constant LIBS already defined [CORE/cake/config/paths.php, line 105]
Notice (8): Constant CSS already defined [CORE/cake/config/paths.php, line 110]
Notice (8): Constant JS already defined [CORE/cake/config/paths.php, line 115]
Notice (8): Constant IMAGES already defined [CORE/cake/config/paths.php, line 120]
Notice (8): Constant CONSOLE_LIBS already defined [CORE/cake/config/paths.php, line 125]
Notice (8): Constant CAKE_TESTS_LIB already defined [CORE/cake/config/paths.php, line 144]
Notice (8): Constant CONTROLLER_TESTS already defined [CORE/cake/config/paths.php, line 149]
Notice (8): Constant COMPONENT_TESTS already defined [CORE/cake/config/paths.php, line 154]
Notice (8): Constant HELPER_TESTS already defined [CORE/cake/config/paths.php, line 159]
Notice (8): Constant MODEL_TESTS already defined [CORE/cake/config/paths.php, line 164]
Notice (8): Constant LIB_TESTS already defined [CORE/cake/config/paths.php, line 169]
Notice (8): Constant LOGS already defined [CORE/cake/config/paths.php, line 181]
Notice (8): Constant CACHE already defined [CORE/cake/config/paths.php, line 186]
Fatal error: require() [http://php.net/function.require]: Cannot redeclare class object in /hogehoge/cake/bootstrap.php on line 33

原因としては コントローラの $uses の値。
モデルを使わないので $uses を false にしてた。
class SomeController extends AppController {
    var $name = 'Some';
    var $uses = false; // ここ

    function index()
    {

    }
}

これが間違い。
正解はこっち。
class SomeController extends AppController {
    var $name = 'Some';
    var $uses = array(); // ここ

    function index()
    {

    }
}

コントローラでモデルを使いたくないのならば、var $uses = array()としてください。これでコントローラに一致するモデルファイルを必要としないコントローラを使うことができるようになります。

まあなんでこうしちゃったかって言うと、
モデルでテーブルを使いたくない時は
class Example extends AppModel {
    var $useTable = false; // このモデルはデータベーステーブルを使用しない
}
とモデルの場合は false なんですよね。

ごっちゃになって覚えてたみたいです。