原则 2.3 实体生成器:示例、文档



Doctrine可以根据小数据输入生成实体,包括关系信息。但是,我未能找到有关该主题的任何示例或简明信息。官方文档只有一小段,有一个小的命令行示例,绝对没有解释任何东西,

任何人都可以参考一本书、文章甚至代码示例吗?

首先你需要引导Doctrine并获取EntityManager的实例,然后一旦你有了它,你可以执行以下操作:

    $cmf = new DoctrineORMToolsDisconnectedClassMetadataFactory();
    $cmf->setEntityManager($em); // $em is EntityManager instance
    $metadata = $cmf->getAllMetadata();
    $generator = new DoctrineORMToolsEntityGenerator();
    $generator->setGenerateAnnotations(true);
    $generator->setGenerateStubMethods(true);
    $generator->setRegenerateEntityIfExists(true);
    $generator->setUpdateEntityIfExists(false);
    $generator->generate($metadata, '/path/to/entities');

有关配置的详细信息,请阅读以下内容:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html

我通常在 yml 中声明元数据,最初从那里生成类和数据库。

下面是一个完整的示例以及 yml 元数据文件:

//example.php
   function getDbConfig()
   {
      //An example configuration
      return array(
         'driver'   => 'pdo_mysql',
         'user'     => 'root',
         'password' => 'potatoes',
         'dbname'   => 'garden',
         'host'     => 'localhost',
         'charset' => 'utf8',
               'driverOptions' => array(
                  1002=>'SET NAMES utf8'
               )
      );
   }
   function bootstrapDoctrine()
   {
      require_once ($this->_libDir . DS . 'Doctrine/ORM/Tools/Setup.php');
      DoctrineORMToolsSetup::registerAutoloadDirectory('/full/path/to/lib');//So that Doctrine is in /full/path/to/lib/Doctrine   
   }
   function getEntityFolders()
   {
      //An example configuration of two entity folders
      return array(
         '/full/path/to/App/Module1/Entities/yml' => '\App\Module1\Entities',
         '/full/path/to/App/Module2/Entities/yml' => '\App\Module2\Entities'
      );
   }
   function setupDoctrine()
   {
      $config = DoctrineORMToolsSetup::createConfiguration();
      $driver = new DoctrineORMMappingDriverSimplifiedYamlDriver(getEntityFolders());
      $driver->setGlobalBasename('schema');
      $config->setMetadataDriverImpl($driver);
      $entityManager = DoctrineORMEntityManager::create($dbConfig, $config);
      return $entityManager;
   }
   function getEntitiesMetaData($em)
   {
      $cmf = new DoctrineORMToolsDisconnectedClassMetadataFactory();
      $cmf->setEntityManager($em);  // we must set the EntityManager
      $driver = $em->getConfiguration()->getMetadataDriverImpl();
      $classes = $driver->getAllClassNames();
      $metadata = array();
      foreach ($classes as $class) {
        //any unsupported table/schema could be handled here to exclude some classes
        if (true) {
          $metadata[] = $cmf->getMetadataFor($class);
        }
      }
      return $metadata;
   }
   function generateEntities($rootDir, $metadata)
   {
      $generator = new DoctrineORMToolsEntityGenerator();
      $generator->setUpdateEntityIfExists(true);    // only update if class already exists
      //$generator->setRegenerateEntityIfExists(true);  // this will overwrite the existing classes
      $generator->setGenerateStubMethods(true);
      $generator->setGenerateAnnotations(true);
      $generator->generate($metadata, $rootDir);
   }
   function generateDatabase()
   {
      $schema = new DoctrineORMToolsSchemaTool($em);
      $schema->createSchema($metadata);
   }
   //Sets up the Doctrine classes autoloader
   bootstrapDoctrine();
   //Sets up database connection, schema files (yml) and returns the EntityManager
   $em = setupDoctrine();
   //Returns the metadata specified by the two schema.orm.yml files
   $metadata = getEntitiesMetaData($em);
   /* Generates the classes based on the yml schema. Using the yml files in the example
    * the result will be the following files:
    *    /full/path/to/App/Module1/Entities/User.php
    *    /full/path/to/App/Module2/Entities/Comment.php
    *    /full/path/to/App/Module2/Entities/Page.php
    */
   generateEntities('/full/path/to', $metadata);
   //Now generate database tables:
   generateDatabase($metadata);

以及示例架构文件:模块 1 的 YAML 架构:

  # /full/path/to/App/Module1/Entities/yml/schema.orm.yml
  AppModule1EntitiesUser:
    type: entity
    table: user
    id:
      id:
        type: integer
        generator:
          strategy: AUTO
    fields:
      name:
      email:

模块 2 的 YAML 架构:

  # /full/path/to/App/Module2/Entities/yml/schema.orm.yml
  AppModule2EntitiesPage:
    type: entity
    table: page
    id:
      id:
        type: integer
        generator:
          strategy: AUTO
    fields:
      name:
      content:
        type: text
  AppModule2EntitiesComment:
    type: entity
    table: comment
    id:
      id:
        type: integer
        generator:
          strategy: AUTO
    fields:
      comment_date:
        type: datetime
      content:
        type: text
    manyToOne:
      user:
        targetEntity: AppModule1EntitiesUser

请注意,我使用的是简化YamlDriver元数据驱动程序,它可以从单个yml文件加载多个实体。

这是一个工作示例,我使用这些确切的步骤从 yml 生成类文件和 db。此外,入门教程包含相当多的 yml 示例。

相关内容

  • 没有找到相关文章

最新更新