cakeHP致命错误:无法重新声明类AppModel


cakePHP Fatal error: Cannot redeclare class AppModel

环境

  • PHP 5.4.17
  • MySQL 5.5
  • CakePHP 2.4.3
  • 作曲家
  • Windows 7 x64
  • Git Bash(终端)

问题

我在composer中使用CakePHP,一切都很正常。

但是,当我尝试使用cake bake并选择选项[V] View时,我会得到这个错误(见下文):

$ app/Console/cake bake
Welcome to CakePHP v2.4.3 Console
---------------------------------------------------------------
App : app
Path: c:'workspace'site'src'app'
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[F]ixture
[T]est case
[Q]uit
What would you like to Bake? (D/M/V/C/P/F/T/Q)
> v
---------------------------------------------------------------
Bake View
Path: c:'workspace'site'src'app'View'
---------------------------------------------------------------
Use Database Config: (default/test)
[default] >
Possible Controllers based on your current database:
---------------------------------------------------------------
 1. Groups
 2. Navigations
 3. PageImages
 4. Pages
 5. Sections
 6. Sliders
 7. Users
Enter a number from the list above,
type in the name of another controller, or 'q' to exit
[q] > 1
Would you like bake to build your views interactively?
Warning: Choosing no will overwrite Groups views if it exist. (y/n)
[n] > y
Would you like to create some CRUD views
(index, add, view, edit) for this controller?
NOTE: Before doing so, you'll need to create your controller
and model classes (including associated models). (y/n)
[y] > n
Would you like to create the views for admin routing? (y/n)
[n] > y
PHP Fatal error:  Cannot redeclare class AppModel in C:'workspace'site'Vendor'pear-pear.cakephp.org'CakePHP'Cake'Test'Case'Model'mode
ls.php on line 57

引导程序.php

<?php
require ROOT . DS . 'Vendor/autoload.php';
// Remove and re-prepend CakePHP's autoloader as Composer thinks it is the
// most important.
// See: http://goo.gl/kKVJO7
spl_autoload_unregister(array('App', 'load'));
spl_autoload_register(array('App', 'load'), true, true);

我该怎么解决?

错误消息很清楚:您不知怎么加载了两次AppModel。

Cannot redeclare class AppModel in C:'workspace'site'Vendor'pearpear.cakephp.org'CakePHP'Cake'Test'Case'Model'models.php on line 57

意味着AppModel已经加载到其他地方。我的猜测是,您已经安装了两个CakePHP。路径听起来像是你通过梨安装的,你的文本说你也使用了composer。因此,我猜脚本不知何故被加载了两次,当它出于任何原因试图第二次加载时,就会导致错误。你必须弄清楚第一次加载类的位置,你可以使用反射来弄清楚。

$AppModel = new AppModel(/*...*/);
$Reflection = new ReflectionClass(get_class($AppModel ));
debug(dirname($Reflection->getFileName());

此外,我猜自动加载器会先启动,然后,出于某种原因,我不知道它也会尝试从Pear安装中加载内核。

即使我像Mark一样重新注册Cake Autoloader,我在使用auth组件时也会遇到自动加载冲突,因为模型组、用户和产品在Cake/Test/Case/Model/Models.php 中进行了第二次定义

我通过使用composer钩子脚本并删除composers autoload_classmap 中的映射来解决这个问题

composer.json:

....
"scripts": {
        "post-autoload-dump": [
            "./composer_fix_autoload.sh"
        ]
    },
....

composer_fix_autoload.sh:

#!/bin/bash
mv vendors/composer/autoload_classmap.php vendors/composer/autoload_classmap.php.bak
sed '/CakePHP'/Cake'/Test'/Case'/Model'/models'.php/d' vendors/composer/autoload_classmap.php.ori > vendors/composer/autoload_classmap.php

我发现,当您在烘焙视图的模型中有一些关联,而关联的模型类还没有烘焙时,就会发生这种情况。

然后以某种方式使用来自autoload_classmap.php:的映射

'AppModel' => $vendorDir . '/pear-pear.cakephp.org/CakePHP/Cake/Test/test_app/Model/AppModel.php',

不幸的是,我的知识太少,无法修复这个错误。