doctrine1 icon indicating copy to clipboard operation
doctrine1 copied to clipboard

Passing Options to Connection and other Issue

Open anvaya opened this issue 4 years ago • 1 comments

Hi,

We were trying to enable SSL for our MySQL connection by setting up PDO::MYSQL_ATTR_SSL_CA and other attributes, turns out there is no way to achieve it using databases.yml. While there seems to be attributes and an a vague "other" option being used in code, nothing gets passed to the actual connection.

lexpress/doctrine1/lib/Doctrine/Connection.php Line 215 in constructor:

$this->options['other'] = array();
if (isset($adapter['other'])) {
    $this->options['other'] = array(Doctrine_Core::ATTR_PERSISTENT => $adapter['persistent']);
}

First of all, if $adaptor["other"] is being checked, not sure why values in $adaptor["other"] are not being passed on to this->options. Secondly, why would $adaptor["other"] guarantee the presence of $adaptor["persistent"] is beyond me. Clearly seems to be an error.

There is also an issue with getOption method code (Line 262). If the option is not set, the code has no return value, making it a method that can be void and returning a value at the same time. Shouldn't it return null or false?

/**
     * getOption
     *
     * Retrieves option
     *
     * @param string $option
     * @return void
     */
    public function getOption($option)
    {
        if (isset($this->options[$option])) {
            return $this->options[$option];
        }
    }

We had to ultimately use the event doctrine.configure_connection in order to add PDO::MYSQL_ATTR_SSL_CA to the connection's other property in order to get the SSL connection. I think there has to be some way to pass additional options to the connection through databases.yml.

anvaya avatar Aug 12 '21 11:08 anvaya

Hello @anvaya,

Problematic

As I understand, you want to use the SSL connection.

As you said, currently, Doctrine1 does not support SSL connection.

There is the list of supported PDO attributes,

Possible solution

Extends doctrine to add the feature.

  1. Extends the doctrine driver used

    class My_Connection_Mysql extends Doctrine_Connection_Mysql
    {
        public function __construct(Doctrine_Manager $manager, $adapter, $user = null, $pass = null)
        {
             parent::__construct($manager, $adapter, $user, $pass);
    
            // here you can hydrate `$this->options['other']` from parameters of $adapter
        }
    }
    
  2. Listen to doctrine.configure event (the subject is an instance of Doctrine_Manager)

    Doctrine_Manager::registerConnectionDriver('mysql', 'My_Connection_Mysql')
    
  3. You can pass option to the DSN on attributes.yml.

        dsn: 'mysql://user:pass@localhost/foo_dbname?key=client-key.pem&cert=client-cert.pem'
    

Example

With

    dsn: 'mysql://localhost/jobeet?foo=bar&key=foo'

$adapter

array:10 [▼
  "scheme" => "mysql"
  "host" => "localhost"
  "path" => "/jobeet"
  "query" => "foo=bar&key=foo"
  "dsn" => "mysql:host=localhost;dbname=jobeet"
  "port" => null
  "user" => null
  "pass" => null
  "fragment" => null
  "database" => "jobeet"
]

alquerci avatar Aug 15 '21 17:08 alquerci