symfony2 + doctrine2@postgresql设置模式

时间:2022-10-16 18:11:11

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.

我想用symfony2+doctrine2来做一个新项目。我遇到了一个关于postgresql计划的小问题。与mysql相比,您可以在postgres(与其他数据库一样)中指定不同的方案。例如,我们的productiv数据库有大约200个方案。

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:

几个月前,我在另一个项目中解决了这个问题,该项目只使用doctrine2。我做了以下几点:

$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?

但是我不知道在symfony2里该怎么做?

2 个解决方案

#1


4  

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:

您可以尝试实现和使用您自己的driver_class并在PDO驱动程序选项中传递search_path,例如在symfony配置中:

# 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.

需要使用_constructPdoDsn方法,因为它在\Doctrine\DBAL\驱动程序\PDOPgSql驱动程序中没有定义为protected。这有点“庸俗”,因为我们正在使用PDO驱动选项,我不确定这是否是一个好方法——但它似乎是有效的。

Hope this helps.

希望这个有帮助。

Best regards,

最好的问候,

Patryk

Patryk

#2


0  

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

因为Doctrine 2.5可以在@Table注释中指定模式名:

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

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

唯一的缺点是,symfony控制台不能这么做,您必须手动指定它。

#1


4  

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:

您可以尝试实现和使用您自己的driver_class并在PDO驱动程序选项中传递search_path,例如在symfony配置中:

# 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.

需要使用_constructPdoDsn方法,因为它在\Doctrine\DBAL\驱动程序\PDOPgSql驱动程序中没有定义为protected。这有点“庸俗”,因为我们正在使用PDO驱动选项,我不确定这是否是一个好方法——但它似乎是有效的。

Hope this helps.

希望这个有帮助。

Best regards,

最好的问候,

Patryk

Patryk

#2


0  

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

因为Doctrine 2.5可以在@Table注释中指定模式名:

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

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

唯一的缺点是,symfony控制台不能这么做,您必须手动指定它。