运行原则转换映射时跳过表


skip tables when running Doctrine convert-mapping

我第一次坐下来研究教义,并尝试在现有站点/数据库上使用它的一些命令行工具。我已经从命令行使用了映射和实体逆向工程工具,并想看看它会从 php/mysql 中的现有网站生成什么样的文件。

但是,它会在任何异常时停止。我已经能够为 enum 创建一个类型映射,以便暂时将它们扔到字符串中,但现在它阻止了我未指定主 ID 键的表。我对在链接表中使用主键或复合键的讲座不感兴趣,我现在只是想在学习教义时生成一些输出。

有没有办法告诉教义跳过抛出异常的表格,或者在它击中它们时不停地通过?我目前只想要一些示例输出,不想完全重构一个复杂的数据库,只是为了看看 Doctrine 可以做什么。

西 南部

Doctrine 转换和验证的主要问题是,默认情况下它会读取整个数据库和所有表,而不管它们的实体或映射是否存在。即使在调用orm:convert:mappingorm:generate:entities时使用--filter=""标志

若要解决此问题并让 Doctrine 跳过引发异常的表,可以将 Doctrine 设置为仅读取所需的表,方法是使用 setFilterSchemaAssetsExpression

$isDev = true;
$config = 'Doctrine'ORM'Tools'Setup::createAnnotationMetadataConfiguration(array('/path/to/entities'), $isDev);
$config->setFilterSchemaAssetsExpression('/^(table1|table2|prefixed_.*|.*_suffixed)$/');
$em =  'Doctrine'ORM'EntityManager::create(array(
    'driver' => 'db_driver',
    'host' => 'localhost',
    'user' => 'user_name',
    'password' => 'user_password',
    'dbname' => 'database',
), $config);

或者,您可以为现有实体管理器的连接设置筛选器。

$em->getConnection()->setFilterSchemaAssetsExpression('/^(filtered_table)$/');

参考:http://doctrine-orm.readthedocs.org/en/latest/reference/configuration.html

当然,这不是告诉原则忽略异常的一种方式,但应该可以帮助您开始学习某些特定表而不是整个数据库上的现有应用程序。


作为旁注,因为您提到您有链接表。如果表中的外键未包含在筛选器中,则 --from-database 转换仍将为其创建引用。您必须手动将映射更改为其原始形式,而不是其OneToMany,ManyToOne或ManyToMany的关联映射。

doctrine orm:convert:mapping --from-database annotation /path/to/entities进行批注映射

/**
 * @ORM'Entity
 * @ORM'Table(name="order")
 */
class Order
{
  /** 
   * @var 'Customer
   * @ORM'ManyToOne(targetEntity="Customer")
   * @ORM'JoinColumns({
   *   @ORM'JoinColumn(name="customer", referencedColumnName="id", nullable=true)
   * })
   */
   private customer;
}

/**
 * @ORM'Entity
 * @ORM'Table(name="order")
 */
class Order
{
  /** 
   * @var integer
   * @ORM'Column(name="customer", type="integer", nullable=true)
   */
   private customer;
}

然后不要忘记使用 doctrine orm:validate-schema 来确保映射同步。

再次重新审视同样的问题。我发现似乎有效的是通过引导程序文件在 cli 配置中使用过滤:

// bootstrap.php
require_once("vendor/autoload.php");
use Doctrine'ORM'Tools'Setup;
use Doctrine'ORM'EntityManager;
$site_root = realpath(__DIR__ . "/../");
$paths = [ $site_root . "/libraries/Doctrine/Entities" ];
$isDevMode = false;
// used when building entities from database using command line tool
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$filter_include = [
    'some_table_name',
    'another_table_name',
    '.*some_sub_portion.*',
    '^some_table_prefix_.*',
    '.*some_table_suffix$'
];
$include_reg = '/^(' . implode('|', $filter_include) . ').*$/';
$filter_exclude = [
    // tables with no primary key generate exceptions
    'some_table_name',
    'another_table_name',
    '.*some_sub_portion.*',
    '^some_table_prefix_.*',
    '.*some_table_suffix$'
];
$exclude_reg = '/^(?!(?:' . implode('|', $filter_exclude) . ')$).*$/';
// use include list
//$config->setFilterSchemaAssetsExpression($include_reg);
// use exclude list
$config->setFilterSchemaAssetsExpression($exclude_reg);
// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'myuser',
    'password' => 'mypassword',
    'dbname'   => 'mydatabase',
);
$entityManager = EntityManager::create($dbParams, $config);
$platform = $entityManager->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');

不久前我遇到了同样的问题。完全按照相同的顺序。首先,我必须将枚举映射到字符串,然后它抱怨缺少主键。

我很确定没有办法忽略异常。或者至少我在文档中或在线都找不到。所以我所做的是为所有没有主键的表添加一个主键。喜欢更改表添加temp_id int 主键不为空auto_increment;

然后我从那里开始,看看哪些实体要保留,哪些实体要删除。否则,您可以创建数据库的副本并删除没有主键的表。但是,如果数据库有很多表,则可能需要一段时间才能手动操作,您应该考虑制作一个过程