2

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

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

4

2 回答 2

5

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

    $cmf = new Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
    $cmf->setEntityManager($em); // $em is EntityManager instance
    $metadata = $cmf->getAllMetadata();
    $generator = new \Doctrine\ORM\Tools\EntityGenerator();
    $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

于 2012-10-26T10:13:46.143 回答
1

我通常在 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');
      Doctrine\ORM\Tools\Setup::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 = Doctrine\ORM\Tools\Setup::createConfiguration();
      $driver = new \Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver(getEntityFolders());
      $driver->setGlobalBasename('schema');
      $config->setMetadataDriverImpl($driver);

      $entityManager = \Doctrine\ORM\EntityManager::create($dbConfig, $config);
      return $entityManager;
   }

   function getEntitiesMetaData($em)
   {
      $cmf = new Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
      $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 Doctrine\ORM\Tools\EntityGenerator();
      $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 Doctrine\ORM\Tools\SchemaTool($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
  App\Module1\Entities\User:
    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
  App\Module2\Entities\Page:
    type: entity
    table: page
    id:
      id:
        type: integer
        generator:
          strategy: AUTO
    fields:
      name:
      content:
        type: text

  App\Module2\Entities\Comment:
    type: entity
    table: comment
    id:
      id:
        type: integer
        generator:
          strategy: AUTO
    fields:
      comment_date:
        type: datetime
      content:
        type: text
    manyToOne:
      user:
        targetEntity: App\Module1\Entities\User

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

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

于 2012-11-11T22:16:11.773 回答