Doctrine 2.3实体生成器:样本,文档?

时间:2012-10-05 21:35:30

标签: php doctrine doctrine-orm

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

任何人都可以参考一本书,文章甚至代码样本吗?

2 个答案:

答案 0 :(得分:4)

首先你需要bootstrap Doctrine and get an instance of 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

答案 1 :(得分: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);

示例模式文件: module1的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。此外,Getting Started tutorial包含相当多的yml示例。

相关问题