簡體   English   中英

PHP致命錯誤:無法重新聲明類AppModel

[英]PHP Fatal error: Cannot redeclare class AppModel

環保

  • 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);

我該如何解決?

錯誤消息很明顯:您以某種方式兩次加載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安裝中加載內核。

即使當我像Mark一樣重新注冊Cake Autoloader時,在使用auth組件時也會遇到Autoloading沖突,因為Models組,User和Product在Cake / Test / Case / Model / Models.php中第二次定義

我通過使用作曲家掛鈎腳本並刪除了作曲家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',

不幸的是,我了解的很少,無法修復此錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM