user2839159
user2839159

Reputation: 463

Doctrine2 Ignore table of database

I'm using Doctrine 2 and I want to generate an ORM of my database but I don't want select all tables of the db.

For example, in this db :

I want to choose ONLY Table 2 with this command:

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

I have an error :

Table Table_1 has no primary key. Doctrine does not support reverse engineering from tables that don't have a primary key.

Ok I know , but I don't want my table 1 in my ORM. When my table 1 has primary key i can filter the tables. I've seen Generating a single Entity from existing database using symfony2 and doctrine, but it doesn't work.

Upvotes: 13

Views: 13545

Answers (4)

Mike Doe
Mike Doe

Reputation: 17576

In recent versions of the Doctrine bundle one has to configure schema filter on the connection level, so:

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

Upvotes: 4

Stephan Vierkant
Stephan Vierkant

Reputation: 10144

Ignoring the table was the solution:

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

Upvotes: 6

Zolt&#225;n S&#252;le
Zolt&#225;n S&#252;le

Reputation: 1694

If you use Doctrine2 without Symfony then you should add this line to your bootstrap:

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

the whole bootstrap looks like

<?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__)~");

Upvotes: 4

Laurynas Mališauskas
Laurynas Mališauskas

Reputation: 1919

Doctrine first validates your tables and only then executes the command. So you should always have valid DB schema in order to make any operations with it.

Upvotes: -2

Related Questions