{40} - 3.4.1 Database Configuration

{3366} - 3.4.1 Database Configuration

CakePHP expects database configuration details to be in a file at app/config/database.php. An example database configuration file can be found at app/config/database.php.default. A finished configuration should look something like this.

var $default = array('driver'      => 'mysql',
                     'persistent'  => false,
                     'host'        => 'localhost',
                     'login'       => 'cakephpuser',
                     'password'    => 'c4k3roxx!',
                     'database'    => 'my_cakephp_project',
                     'prefix'      => '');
  1. var $default = array('driver' => 'mysql',
  2. 'persistent' => false,
  3. 'host' => 'localhost',
  4. 'login' => 'cakephpuser',
  5. 'password' => 'c4k3roxx!',
  6. 'database' => 'my_cakephp_project',
  7. 'prefix' => '');

The $default connection array is used unless another connection is specified by the $useDbConfig property in a model. For example, if my application has an additional legacy database in addition to the default one, I could use it in my models by creating a new $legacy database connection array similar to the $default array, and by setting var $useDbConfig = ‘legacy’; in the appropriate models.

Fill out the key/value pairs in the configuration array to best suit your needs.

Key Value
driver The name of the database driver this configuration array is for. Examples: mysql, postgres, sqlite, pear-drivername, adodb-drivername, mssql, oracle, or odbc.
persistent Whether or not to use a persistent connection to the database.
host The database server’s hostname (or IP address).
login The username for the account.
password The password for the account.
database The name of the database for this connection to use.
prefix (optional) The string that prefixes every table name in the database. If your tables don’t have prefixes, set this to an empty string.
port (optional) The TCP port or Unix socket used to connect to the server.
encoding Indicates what character set the to use to send SQL statements to the server.
schema Used in PostgreSQL database setups to specify which schema to use.

The prefix setting is for tables, not models. For example, if you create a join table for your Apple and Flavor models, you name it prefix_apples_flavors (not prefix_apples_prefix_flavors), and set your prefix setting to 'prefix_'.

At this point, you might want to take a look at the CakePHP Conventions. The correct naming for your tables (and the addition of some columns) can score you some free functionality and help you avoid configuration. For example, if you name your database table big_boxes, your model BigBox, your controller BigBoxesController, everything just works together automatically. By convention, use underscores, lower case, and plural forms for your database table names - for example: bakers, pastry_stores, and savory_cakes.

Differences

Lines: 1-2Lines: 1-114
 <title>Database Configuration</title> <title>Database Configuration</title>
 +<p>
 + CakePHP expects database configuration details to be in a file at app/config/database.php. An example database configuration file can be found at app/config/database.php.default. A finished configuration should look something like this.
 +</p>
 +<pre>
 +var $default = array('driver' =&gt; 'mysql',
 + 'persistent' =&gt; false,
 + 'host' =&gt; 'localhost',
 + 'login' =&gt; 'cakephpuser',
 + 'password' =&gt; 'c4k3roxx!',
 + 'database' =&gt; 'my_cakephp_project',
 + 'prefix' =&gt; '');
 +</pre>
 +<p>
 + The $default connection array is used unless another connection is specified by the $useDbConfig property in a model. For example, if my application has an additional legacy database in addition to the default one, I could use it in my models by creating a new $legacy database connection array similar to the $default array, and by setting var $useDbConfig = ‘legacy’; in the appropriate models.
 +</p>
 +<p>
 + Fill out the key/value pairs in the configuration array to best suit your needs.
 +</p>
 +<table>
 + <tr>
 + <th>
 + Key
 + </th>
 + <th>
 + Value
 + </th>
 + </tr>
 + <tr>
 + <td>
 + driver
 + </td>
 + <td>
 + The name of the database driver this configuration array is for. Examples: mysql, postgres, sqlite, pear-drivername, adodb-drivername, mssql, oracle, or odbc.
 + </td>
 + </tr>
 + <tr>
 + <td>
 + persistent
 + </td>
 + <td>
 + Whether or not to use a persistent connection to the database.
 + </td>
 + </tr>
 + <tr>
 + <td>
 + host
 + </td>
 + <td>
 + The database server’s hostname (or IP address).
 + </td>
 + </tr>
 + <tr>
 + <td>
 + login
 + </td>
 + <td>
 + The username for the account.
 + </td>
 + </tr>
 + <tr>
 + <td>
 + password
 + </td>
 + <td>
 + The password for the account.
 + </td>
 + </tr>
 + <tr>
 + <td>
 + database
 + </td>
 + <td>
 + The name of the database for this connection to use.
 + </td>
 + </tr>
 + <tr>
 + <td>
 + prefix (<em>optional</em>)
 + </td>
 + <td>
 + The string that prefixes every table name in the database. If your tables don’t have prefixes, set this to an empty string.
 + </td>
 + </tr>
 + <tr>
 + <td>
 + port (<em>optional</em>)
 + </td>
 + <td>
 + The TCP port or Unix socket used to connect to the server.
 + </td>
 + </tr>
 + <tr>
 + <td>
 + encoding
 + </td>
 + <td>
 + Indicates what character set the to use to send SQL statements to the server.
 + </td>
 + </tr>
 + <tr>
 + <td>
 + schema
 + </td>
 + <td>
 + Used in PostgreSQL database setups to specify which schema to use.
 + </td>
 + </tr>
 +</table>
 +<p class="note">The prefix setting is for tables, <strong>not</strong> models. For example, if you create a join table for your Apple and Flavor models, you name it prefix_apples_flavors (<strong>not</strong> prefix_apples_prefix_flavors), and set your prefix setting to 'prefix_'.</p>
 +<p>
 + At this point, you might want to take a look at the <a href="/view/22/cakephp-conventions">CakePHP Conventions</a>. The correct naming for your tables (and the addition of some columns) can score you some free functionality and help you avoid configuration. For example, if you name your database table big_boxes, your model BigBox, your controller BigBoxesController, everything just works together automatically. By convention, use underscores, lower case, and plural forms for your database table names - for example: bakers, pastry_stores, and savory_cakes.
 +</p>