CakePHP connect database


To connect to a database in CakePHP, you’ll need to specify the database connection details in the config/app.php file of your application. Here’s an example of how to do this:

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'my_username',
        'password' => 'my_password',
        'database' => 'my_database',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'log' => false,
        'quoteIdentifiers' => false,

You’ll need to set the ‘host’, ‘username’, ‘password’, and ‘database’ values to match your database connection details. If you’re using a different database engine (such as PostgreSQL or SQLite), you’ll also need to set the ‘driver’ value accordingly.

Once you’ve configured your database connection, you can use the CakePHP ORM (Object-Relational Mapper) to interact with your database. For more information about using the ORM in CakePHP, you can refer to the documentation at