Fatal Error with Doctrine while using generate.php

2019-04-17 14:52发布

I'm working on the Doctrine tutorial at http://www.Doctrine-project.org/ and I receive a fatal error when I try to run my generate.php script which makes my models and makes tables in the database:

Fatal error: Class 'BaseCharity' not found in ...\models\Charity.php on line 14

generate.php:

require_once('bootstrap.php');
Doctrine_Core::dropDatabases();
Doctrine_Core::createDatabases();
Doctrine_Core::generateModelsFromYaml('schema.yml', 'models');
Doctrine_Core::createTablesFromModels('models');

and schema.yml

Charity:
  actAs: [Timestampable]
  columns:
    active:
      type: boolean
      default: '1'
    owed: decimal(32,2)
    totalPayed: decimal(32,2)
    name: string(255)
    website: string(255)
    description: text
    icon: string(255)

I am quite stumped by this, I can get it to correctly create other tables that are very similar or much more complicated then this one. I've tried rewriting it as well. I really have no clue where this error is coming from.

2条回答
乱世女痞
2楼-- · 2019-04-17 15:00

Found this:

http://www.doctrine-project.org/jira/browse/DC-344

Hi, I've stumbled upon the same problem and I think I know where the issue is.

So Doctrine_Core::createTablesFromModels() calls Doctrine_Export::exportSchema() which in turn calls Doctrine_Core::loadModels().

Doctrine_Core::loadModels() uses RecursiveIteratorIterator and iterates over all found files.

Now I think the order of files returned by RecursiveIteratorIterator is not always the same (depends on OS, filenames and cosmic radiation ), but the most important thing here is that class files from 'modules/generated' directory (as in examples) ARE NOT included before subclasses derived from generated classes. This means that Doctrine_Core::autoload() fails to load classes from 'modules/generated' directory, exactly this check fails:

if (0 !== stripos($className, 'Doctrine_') || class_exists($className, false) || interface_exists($className, false))

as base class is not starting with 'Doctrine_' and is not yet loaded.

To fix it properly the algorithm for loading modules must be changed to first include 'modules/generated' classes and then rest of classes. I am not sure but maybe Core::autoload() might be changed to include base classes properly.

QUICK WORKAROUND: As a quick workaround I've changed parameters in call to createTablesFromModels() to:

Doctrine_Core::createTablesFromModels(array('models/generated','models'));

as createTablesFromModels() can accept array of directories.

Hope this helps you, please let me know if you need any more information. Thanks!

查看更多
放荡不羁爱自由
3楼-- · 2019-04-17 15:16

You need to register the models with the autoloader provided by Doctrine. No need to use any Iterators or what so ever

Doctrine::loadModels('path/to/your/models');  

You can of course use it sevaral times:

Doctrine::loadModels('path/to/your/models/generated'); 
Doctrine::loadModels('path/to/your/models/custom');    
Doctrine::loadModels('path/to/your/models'); 
查看更多
登录 后发表回答