configuration.rst 8.78 KB
Newer Older
1 2 3 4 5 6 7 8 9
Configuration
=============

Getting a Connection
--------------------

You can get a DBAL Connection through the
``Doctrine\DBAL\DriverManager`` class.

10
.. code-block:: php
11 12 13 14 15 16 17 18 19 20 21

    <?php
    $config = new \Doctrine\DBAL\Configuration();
    //..
    $connectionParams = array(
        'dbname' => 'mydb',
        'user' => 'user',
        'password' => 'secret',
        'host' => 'localhost',
        'driver' => 'pdo_mysql',
    );
22
    $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

The ``DriverManager`` returns an instance of
``Doctrine\DBAL\Connection`` which is a wrapper around the
underlying driver connection (which is often a PDO instance).

The following sections describe the available connection parameters
in detail.

Driver
~~~~~~

The driver specifies the actual implementations of the DBAL
interfaces to use. It can be configured in one of three ways:


-  ``driver``: The built-in driver implementation to use. The
   following drivers are currently available:
40

41 42
   -  ``pdo_mysql``: A MySQL driver that uses the pdo\_mysql PDO
      extension.
43 44
   -  ``drizzle_pdo_mysql``: A Drizzle driver that uses pdo\_mysql PDO
      extension.
45
   -  ``mysqli``: A MySQL driver that uses the mysqli extension.
46 47 48 49 50 51 52
   -  ``pdo_sqlite``: An SQLite driver that uses the pdo\_sqlite PDO
      extension.
   -  ``pdo_pgsql``: A PostgreSQL driver that uses the pdo\_pgsql PDO
      extension.
   -  ``pdo_oci``: An Oracle driver that uses the pdo\_oci PDO
      extension.
      **Note that this driver caused problems in our tests. Prefer the oci8 driver if possible.**
53
   -  ``pdo_sqlsrv``: A Microsoft SQL Server driver that uses pdo\_sqlsrv PDO
54
   -  ``oci8``: An Oracle driver that uses the oci8 PHP extension.
55
   -  ``sqlanywhere``: A SAP Sybase SQL Anywhere driver that uses the sqlanywhere PHP extension.
56 57 58 59 60 61 62 63 64 65 66

-  ``driverClass``: Specifies a custom driver implementation if no
   'driver' is specified. This allows the use of custom drivers that
   are not part of the Doctrine DBAL itself.
-  ``pdo``: Specifies an existing PDO instance to use.

Wrapper Class
~~~~~~~~~~~~~

By default a ``Doctrine\DBAL\Connection`` is wrapped around a
driver ``Connection``. The ``wrapperClass`` option allows to
67
specify a custom wrapper implementation to use, however, a custom
68 69 70 71 72 73 74 75 76 77
wrapper class must be a subclass of ``Doctrine\DBAL\Connection``.

Connection Details
~~~~~~~~~~~~~~~~~~

The connection details identify the database to connect to as well
as the credentials to use. The connection details can differ
depending on the used driver. The following sections describe the
options recognized by each built-in driver.

Benjamin Eberlei's avatar
Benjamin Eberlei committed
78 79 80
.. note::

    When using an existing PDO instance through the ``pdo``
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    option, specifying connection details is obviously not necessary.


pdo\_sqlite
^^^^^^^^^^^


-  ``user`` (string): Username to use when connecting to the
   database.
-  ``password`` (string): Password to use when connecting to the
   database.
-  ``path`` (string): The filesystem path to the database file.
   Mutually exclusive with ``memory``. ``path`` takes precedence.
-  ``memory`` (boolean): True if the SQLite database should be
   in-memory (non-persistent). Mutually exclusive with ``path``.
   ``path`` takes precedence.

pdo\_mysql
^^^^^^^^^^


-  ``user`` (string): Username to use when connecting to the
   database.
-  ``password`` (string): Password to use when connecting to the
   database.
-  ``host`` (string): Hostname of the database to connect to.
-  ``port`` (integer): Port of the database to connect to.
-  ``dbname`` (string): Name of the database/schema to connect to.
-  ``unix_socket`` (string): Name of the socket used to connect to
   the database.
111 112
-  ``charset`` (string): The charset used when connecting to the
   database.
113

114 115
drizzle\_pdo\_mysql
^^^^^^^^^^^^^^^^^^^
116

Steve Müller's avatar
Steve Müller committed
117
**Requires** drizzle plugin ``mysql_protocol`` or ``mysql_unix_socket_protocol`` to be enabled.
118 119
On Ubuntu this can be done by editing ``/etc/drizzle/conf.d/mysql-protocol.cnf``
or ``/etc/drizzle/conf.d/mysql-unix-socket-protocol.cnf`` and restart drizzled daemon.
120 121 122 123 124 125 126 127 128 129 130

-  ``user`` (string): Username to use when connecting to the
   database. Only needed if authentication is configured for drizzled.
-  ``password`` (string): Password to use when connecting to the
   database. Only needed if authentication is configured for drizzled.
-  ``host`` (string): Hostname of the database to connect to.
-  ``port`` (integer): Port of the database to connect to.
-  ``dbname`` (string): Name of the database/schema to connect to.
-  ``unix_socket`` (string): Name of the socket used to connect to
   the database.

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
mysqli
^^^^^^


-  ``user`` (string): Username to use when connecting to the
   database.
-  ``password`` (string): Password to use when connecting to the
   database.
-  ``host`` (string): Hostname of the database to connect to.
-  ``port`` (integer): Port of the database to connect to.
-  ``dbname`` (string): Name of the database/schema to connect to.
-  ``unix_socket`` (string): Name of the socket used to connect to
   the database.
-  ``charset`` (string): The charset used when connecting to the
   database.
-  ``driverOptions`` Any supported flags for mysqli found on `http://www.php.net/manual/en/mysqli.real-connect.php`

148 149 150 151 152 153 154 155 156 157 158
pdo\_pgsql
^^^^^^^^^^


-  ``user`` (string): Username to use when connecting to the
   database.
-  ``password`` (string): Password to use when connecting to the
   database.
-  ``host`` (string): Hostname of the database to connect to.
-  ``port`` (integer): Port of the database to connect to.
-  ``dbname`` (string): Name of the database/schema to connect to.
159 160
-  ``charset`` (string): The charset used when connecting to the
   database.
161 162 163 164
-  ``sslmode`` (string): Determines whether or with what priority
   a SSL TCP/IP connection will be negotiated with the server.
   See the list of available modes:
   `http://www.postgresql.org/docs/9.1/static/libpq-connect.html#LIBPQ-CONNECT-SSLMODE`
165

166 167 168 169 170
PostgreSQL behaves differently with regard to booleans when you use
``PDO::ATTR_EMULATE_PREPARES`` or not. To switch from using ``'true'``
and ``'false'`` as strings you can change to integers by using:
``$conn->getDatabasePlatform()->setUseBooleanTrueFalseStrings($flag)``.

171 172 173 174 175 176 177 178 179 180 181
pdo\_oci / oci8
^^^^^^^^^^^^^^^


-  ``user`` (string): Username to use when connecting to the
   database.
-  ``password`` (string): Password to use when connecting to the
   database.
-  ``host`` (string): Hostname of the database to connect to.
-  ``port`` (integer): Port of the database to connect to.
-  ``dbname`` (string): Name of the database/schema to connect to.
182 183
-  ``pooled`` (boolean): Whether to enable database resident
   connection pooling.
184 185 186
-  ``charset`` (string): The charset used when connecting to the
   database.

187
pdo\_sqlsrv
188
^^^^^^^^^^^
189 190 191 192 193 194 195 196 197 198


-  ``user`` (string): Username to use when connecting to the
   database.
-  ``password`` (string): Password to use when connecting to the
   database.
-  ``host`` (string): Hostname of the database to connect to.
-  ``port`` (integer): Port of the database to connect to.
-  ``dbname`` (string): Name of the database/schema to connect to.

199
sqlanywhere
200
^^^^^^^^^^^
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225


-  ``user`` (string): Username to use when connecting to the
   database.
-  ``password`` (string): Password to use when connecting to the
   database.
-  ``server`` (string): Name of a running database server to connect to.
-  ``host`` (string): Hostname of the database to connect to.
-  ``port`` (integer): Port of the database to connect to.
-  ``dbname`` (string): Name of the database/schema to connect to.
-  ``persistent`` (boolean): Whether to establish a persistent connection.

Depending on the used underlying platform version, you can specify
any other connection parameter that is supported by the particular
platform version via the ``driverOptions`` option.
You can find a list of supported connection parameters for each
platform version here:

- `SQL Anywhere 10.0.1 <http://dcx.sybase.com/index.html#1001/en/dbdaen10/da-conmean.html>`_
- `SQL Anywhere 11.0.0 <http://dcx.sybase.com/index.html#1100/en/dbadmin_en11/conmean.html>`_
- `SQL Anywhere 11.0.1 <http://dcx.sybase.com/index.html#1101/en/dbadmin_en11/conmean.html>`_
- `SQL Anywhere 12.0.0 <http://dcx.sybase.com/index.html#1200/en/dbadmin/da-conparm.html>`_
- `SQL Anywhere 12.0.1 <http://dcx.sybase.com/index.html#1201/en/dbadmin/da-conparm.html>`_
- `SAP Sybase SQL Anywhere 16.0 <http://dcx.sybase.com/index.html#sa160/en/dbadmin/da-conparm.html>`_

226 227 228 229 230 231 232 233 234 235 236 237
Custom Platform
~~~~~~~~~~~~~~~

Each built-in driver uses a default implementation of
``Doctrine\DBAL\Platforms\AbstractPlatform``. If you wish to use a
customized or custom implementation, you can pass a precreated
instance in the ``platform`` option.

Custom Driver Options
~~~~~~~~~~~~~~~~~~~~~

The ``driverOptions`` option allows to pass arbitrary options
238
through to the driver. This is equivalent to the fourth argument of
239
the `PDO constructor <http://php.net/manual/en/pdo.construct.php>`_.