【问题标题】:Doctrine2 Ignore table of databaseDoctrine2 忽略数据库表
【发布时间】:2013-10-21 08:15:08
【问题描述】:

我正在使用 Doctrine 2,我想为我的数据库生成一个 ORM,但我不想选择数据库的所有表。

例如,在这个 db 中:

  • 表 1 没有主键
  • 表2正常

我想用这个命令只选择表 2:

doctrine:mapping:convert --from-database yml ./src/Application/TestBundle/Resources/config/doctrine/metadata/orm --filter="Table2"

我有一个错误:

表 Table_1 没有主键。 Doctrine 不支持对没有主键的表进行逆向工程。

好的,我知道,但我不希望我的表 1 在我的 ORM 中。当我的表 1 有主键时,我可以过滤表。我见过 Generating a single Entity from existing database using symfony2 and doctrine,但它不起作用。

【问题讨论】:

  • 谢谢!在我的例子中,我只需要将一个表从 MySQL 导入 Symfony。所以我的命令是 php app/console doctrine:mapping:import --force StrategioMainBundle yml 在 config.yml 中:schema_filter: ~^(fundinfo1)~
  • 您能否将解决方案发布为答案并接受它? (这个问题出现在 symfony2 问题的 unanswered 列表中)谢谢。
  • @StephanVierkant 我不是 OP。看起来用户不会编辑问题,我们可以在 wiki 答案中添加答案。
  • 我已将答案发布为社区答案并删除了我的 cmets。
  • @StephanVierkant 我们是否需要在答案中添加一个简短的介绍,以解释它是来自 OP 的解决方案?

标签: symfony filter doctrine-orm


【解决方案1】:

在最新版本的 Doctrine 捆绑包中,必须在连接级别配置模式过滤器,因此:

doctrine:
  dbal:
    default_connection: default
    connections:
      default: # <- your connection name
        url: '%env(DATABASE_URL)%'
        schema_filter: '#^(?!table_to_exclude)#'

【讨论】:

    【解决方案2】:

    忽略表格是解决方案:

    doctrine:
        dbal:
            schema_filter: ~^(?!Table1)~
    

    【讨论】:

      【解决方案3】:

      如果你在没有 Symfony 的情况下使用 Doctrine2,那么你应该将这一行添加到你的引导程序中:

      // With this expression all tables prefixed with Table1 will ignored by the schema tool.
      $entityManager->getConnection()->getConfiguration()->setFilterSchemaAssetsExpression("~^(?!Table1)~");
      

      整个引导程序看起来像

      <?php
      // bootstrap.php
      
      use Doctrine\ORM\Tools\Setup;
      use Doctrine\ORM\EntityManager;
      
      
      // Include Composer Autoload (relative to project root).
      require_once "vendor/autoload.php";
      
      // Create a simple "default" Doctrine ORM configuration for Annotations
      $isDevMode = true;
      $paths = array(__DIR__."/doctrine/entities");
      
      $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
      //$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/doctrine/yaml"), $isDevMode);
      
      // the connection configuration
      $dbParams = array(
        'driver'   => 'pdo_mysql',
        'user'     => 'username',
        'password' => 'password',
        'dbname'   => 'database',
      );
      
      /** @var $entityManager \Doctrine\ORM\EntityManager */
      $entityManager = EntityManager::create($dbParams, $config);
      
      
      // Set the other connections parameters
      $conn = $entityManager->getConnection();
      
      
      $platform = $conn->getDatabasePlatform();
      $platform->registerDoctrineTypeMapping('enum', 'string');
      
      
      // With this expression all tables prefixed with t_ will ignored by the schema tool.
      $conn->getConfiguration()->setFilterSchemaAssetsExpression("~^(?!t__)~");
      

      【讨论】:

      • 如果您使用 Symfony 但需要让您的实体共享来自 Laravel 应用程序数据库的表,而该数据库中有一些没有主键的表?
      • 每个实体类都必须有一个标识符/主键:docs.doctrine-project.org/en/latest/reference/…
      【解决方案4】:

      Doctrine 首先验证您的表,然后才执行命令。 因此,您应该始终拥有有效的数据库架构,以便对其进行任何操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-20
        • 1970-01-01
        • 1970-01-01
        • 2013-09-11
        • 1970-01-01
        • 2016-09-19
        • 2015-04-07
        • 1970-01-01
        相关资源
        最近更新 更多