10

I want to use symfony2+doctrine2 for a new project. I ran into a little issue with postgresql-schemes. In contrast to mysql you can specify in postgres (like other databases) different schemes. Our productiv database has around 200 schemes for example.

I have to set a schema for my current doctrine connection. How can I do that?

I solved this issue a few months ago in another project, that uses doctrine2 only. I did the following:

$em = Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$em->getConnection()->exec('SET SEARCH_PATH TO foobar');

But I dont know where I should do that in symfony2?

5
  • Symfony2 just uses doctrine, you should be able to solve it the exact same way. Commented May 18, 2012 at 14:44
  • yeah. but i dont know where i should hook into :( im absolutely new to symfony2... Commented May 18, 2012 at 15:02
  • Look for the EntityManager as well. If nothing goes, grep is your friend. Commented May 18, 2012 at 22:05
  • yeah, but i dont want to modify the symfony2 files :) so i think i have to add some special code to a special place. maybe i need to add a bundle that inherits from the doctrine bundle? Commented May 30, 2012 at 13:07
  • I don't know, but I'll place a bounty on your question, maybe it helps to gain some attention. Commented May 30, 2012 at 13:08

3 Answers 3

5
+100

you could try to implement and use your own driver_class and pass the search_path in the PDO DriverOptions, e.g. in your symfony config:

# Doctrine Configuration
doctrine:
    dbal:
        driver:   pdo_pgsql
        driver_class: YourNamespace\YourBundle\Doctrine\DBAL\Driver\PDOPgSql\Driver
        options:
            search_path: YOUR_SEARCH_PATH

The driver could look something like this:

namespace YourNamespace\YourBundle\Doctrine\DBAL\Driver\PDOPgSql;

use Doctrine\DBAL\Platforms;

class Driver extends \Doctrine\DBAL\Driver\PDOPgSql\Driver implements \Doctrine\DBAL\Driver
{
    public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
    {
        // ADD SOME ERROR HANDLING WHEN THE SEARCH_PATH IS MISSING...
        $searchPath = $driverOptions['search_path'];
        unset($driverOptions['search_path']);

        $connection = new \Doctrine\DBAL\Driver\PDOConnection(
            $this->_constructPdoDsn($params),
            $username,
            $password,
            $driverOptions
        );

        $connection->exec("SET SEARCH_PATH TO {$searchPath};");

        return $connection;
    }

    /**
     * Constructs the Postgres PDO DSN.
     *
     * @return string The DSN.
     */
    protected function _constructPdoDsn(array $params)
    {
        $dsn = 'pgsql:';
        if (isset($params['host']) && $params['host'] != '') {
            $dsn .= 'host=' . $params['host'] . ' ';
        }
        if (isset($params['port']) && $params['port'] != '') {
            $dsn .= 'port=' . $params['port'] . ' ';
        }
        if (isset($params['dbname'])) {
            $dsn .= 'dbname=' . $params['dbname'] . ' ';
        }

        return $dsn;
    }
}

You need the _constructPdoDsn method because it isn't defined as protected in \Doctrine\DBAL\Driver\PDOPgSql\Driver. It's bit "hacky" because we are using PDO DriverOptions and i'm not sure if that's a good way - but it seems to work.

Hope this helps.

Best regards,

Patryk

Sign up to request clarification or add additional context in comments.

2 Comments

Why not extend from \Doctrine\DBAL\Driver\PDOPgSql\Driver.php?
The class Doctrine\DBAL\Driver\PDOPgSql\Driver is declared as final and therefore cannot be derived. However, the customer driver could be built as a wrapper class around the original driver in order to use all of its official options.
1

Since Doctrine 2.5 you can specify the schema name in the @Table annotation:

/**
 * Clerk
 *
 * @Table(schema="schema")
 */
class Clerk { }

The only downside is, the symfony console can't do that, you have to specify it by hand.

1 Comment

This is not working for table migrations and this is not excluding others schemas from doctrine management.
0

In stack PostgreSQL, Symfony 6.4.3 and Doctrine 2.17 you can use attribute for set schema name in Entity class.

Example

#[ORM\Table(schema: 'SCHEMA_NAME')]

But when you make migrations, migration versions table created in public schema, for fix it, you need add this parameters to doctrine_migrations.yaml

storage:
  # Default (SQL table) metadata storage configuration
  table_storage:
    table_name: '%env(resolve:SCHENA_NAME)%.doctrine_migration_versions'

Also you need to .env file variable SCHEMA_NAME and set value for this variable.

Sorry for my English, it's not my native language. Hope it's help sombody.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.