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

时间:2013-12-06 12:22:41

标签: php cakephp composer-php

的Env

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

问题

我正在将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

bootstrap.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);

我该如何解决?

3 个答案:

答案 0 :(得分:4)

错误消息很明确:您正在以某种方式加载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安装。路径听起来就像你通过Pear安装它,你的文字说你也使用过作曲家。所以我猜这个脚本以某种方式加载了两次,当它因任何原因试图第二次加载它时会导致错误。你必须弄清楚第一次加载类的位置,你可以使用反射来计算出来。

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

此外我猜自动加载器首先启动然后,由于某种原因我不知道它也试图从Pear安装加载核心。

答案 1 :(得分:2)

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

我通过使用composer钩子脚本并删除了作曲家中的映射来解决了这个问题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

答案 2 :(得分:1)

我发现,当你在模型中有一些关联你正在烘焙视图并且相关的模型类还没有被烘焙时,它就会发生。

然后以某种方式使用autoload_classmap.php

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

不幸的是,我没有太多知识来修复这个错误。