简体   繁体   English

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

[英]PHP Fatal error: Cannot redeclare class AppModel

Env 环保

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

Question

I'm using CakePHP with composer and everything is works. 我在作曲家中使用CakePHP,一切正常。

But, when I try use cake bake and select option [V] View I get this error (see below): 但是,当我尝试使用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 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);

How can I fix that? 我该如何解决?

The error message is clear: You're loading the AppModel two times somehow. 错误消息很明显:您以某种方式两次加载AppModel。

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

Means that AppModel was loaded somewhere else already. 意味着AppModel已经加载到其他地方。 My guess is that you have two installations of CakePHP. 我的猜测是您有两个CakePHP安装。 The path sounds like you have it installed via Pear and your text says you've used composer as well. 听起来好像您是通过Pear安装了该路径,并且您的文字说您也曾经使用过作曲家。 So I guess the script is somehow loaded two times which causes the error when it tries to load it a second time for whatever reason. 因此,我猜想脚本以某种方式加载了两次,无论出于何种原因尝试再次加载时都会导致错误。 You'll have to figure out from where the class is loaded the first time, you can use reflections to figure that out. 您必须弄清楚第一次加载类的位置,可以使用反射来弄清楚。

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

Further I guess the autoloader kicks in first and then, for a reason I don't know it also tries to load the core from the Pear installation. 此外,我猜想自动加载器会先启动,然后由于某种原因,我不知道它还会尝试从Pear安装中加载内核。

Even when I reregisterd the Cake Autoloader like Mark , I got Autoloading conflicts when using the auth Component, because of the Models Group, User and Product are defined a second time in Cake/Test/Case/Model/models.php 即使当我像Mark一样重新注册Cake Autoloader时,在使用auth组件时也会遇到Autoloading冲突,因为Models组,User和Product在Cake / Test / Case / Model / Models.php中第二次定义

I resolved the issue by using a composer hook script and deleting the mapping within composers autoload_classmap 我通过使用作曲家挂钩脚本并删除了作曲家autoload_classmap中的映射解决了该问题

composer.json: composer.json:

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

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

I've discovered, that it happens, when you have some association(s) in model for which you're baking views and the associated model class isn't baked yet. 我发现,当您在模型中有一些关联要烘焙视图并且关联的模型类尚未烘焙时,就会发生这种情况。

Then somehow is used mapping from autoload_classmap.php : 然后以某种方式使用autoload_classmap.php映射:

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

Unfortunately, I have too little knowledge to fix this bug. 不幸的是,我了解的很少,无法修复此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM