Database Integration
Lithe offers flexibility in connecting to different databases. Integrating seamlessly with popular ORMs like Eloquent, Lithe also supports native PHP drivers like MySQLi and PDO, ensuring broad compatibility with various database management systems.
Environment Setup
To configure the database connection environment in Lithe, you can adjust the environment variables already present in the .env
file at the root of your project:
DB_CONNECTION_METHOD=pdo
DB_CONNECTION=mysql
DB_HOST=localhost
DB_NAME=lithe
DB_USERNAME=root
DB_PASSWORD=
DB_SHOULD_INITIATE=true
Where:
-
DB_CONNECTION_METHOD
: Defines the database connection method used by Lithe. It can be set toeloquent
,pdo
,mysqli
, or other custom connection methods defined in the application. -
DB_CONNECTION
: Specifies the type of database Lithe should connect to. For example,mysql
,pgsql
,sqlite
, among others. -
DB_HOST
: Indicates the address of the server where the database is hosted. Usually set tolocalhost
for local connections or an IP address for remote connections. -
DB_NAME
: The name of the database that Lithe should use to store application data. -
DB_USERNAME
: The username used to authenticate the database connection. -
DB_PASSWORD
: The password associated with the username to authenticate the database connection. If left empty, it indicates that no password is configured for the specified user. -
DB_SHOULD_INITIATE
: Indicates whether Lithe should automatically initiate the database connection when loading the application. It can be set totrue
for automatic initiation orfalse
for manual initialization at specific points in the code.
These settings are crucial for establishing an effective and secure connection with the defined database, ensuring that Lithe can interact with application data appropriately during development and execution.
Remember to replace the placeholders (localhost
, lithe
, root
, and empty password
) with your actual database credentials. It is highly recommended to set a strong password to protect your credentials and ensure the security of the database connection.
Configuration
To configure Lithe to use different database connection methods, including PDO, MySQLi, or Eloquent, follow the steps below:
Define the environment variables in the `.env` file
DB_CONNECTION_METHOD=mysqli
DB_CONNECTION=mysql
Install the corresponding package using Composer
These detailed steps simplify the configuration of each ORM or database driver with Lithe, allowing you to choose and set up the most suitable connection method for your application.
Establishing Connection with the Database
After configuring the .env
file with the necessary variables for connecting to the database, Lithe uses the Manager
class not only to load these settings but also to establish and manage the corresponding connection.
Manager
Class
Connection Management with the The Manager
class in Lithe is a fundamental component for managing database connection operations. It provides a crucial method called initialize
, which establishes the connection, and the method connection
, which returns the configured connection, ready to be used by the application.
The initialize
method prepares and establishes the connection according to the environment variables. This includes setting the connection type (eloquent
, pdo
, mysqli
) and the specific database details (such as host, database name, username, and password).
initialize
Method
Using the In the file src/database/config/database.php
, the initialize
method is already called by default to configure the database connection:
use Lithe\Database\Manager as DB;
DB::initialize();
It is essential that this method is present and called to ensure that the connection is available for operations such as database migrations.
Integrating the Connection in the Application
After initializing the connection using the static method initialize()
of the Manager
class, the configured connection will be accessible through the static method connection
. This enables efficient and secure execution of queries and database operations.
Using Conventional Methods with PDO:
use Lithe\Database\Manager as DB;
$pdo = DB::connection();
$users = $pdo->query('SELECT * FROM users')->fetchAll(PDO::FETCH_ASSOC);
This approach facilitates the integration of Lithe with different database technologies, providing flexibility and efficiency in web application development.
Configuring Other ORMs or Drivers
To use other ORMs or database drivers with Lithe, you can flexibly configure them using the static method configure
of the class Lithe\Database\Manager
in the file src/database/config/database.php
. This method allows you to add new connection configurations in a customized manner.
configure
Method
Structure of the The configure
method takes two parameters:
-
Configuration Name: A unique identifier for your configuration that will be used later to reference this connection.
-
Configuration Callback: A callback function that receives a configuration object as a parameter. Inside the callback, you can define and return the configured instance of your ORM or database driver.
Custom Configuration
In the file src/database/config/database.php
, add a custom configuration as follows:
use Lithe\Database\Manager as DB;
DB::configure('my_custom_orm', function ($config) {
// Specific configuration for your ORM or driver
return new MyCustomORM([
'driver' => $config->driver,
'host' => $config->host,
'database' => $config->name,
'username' => $config->username,
'password' => $config->password,
]);
});
In the example above, my_custom_orm
is the name of the configuration you are creating. The callback receives an array $config
containing the connection details (such as connection type, host, database name, username, and password), which are used to initialize and return the instance of your custom ORM or database driver.
Integration with Environment Variables
After configuring the configure
method in the database.php
file, you need to associate the specific database details with the environment variables in the .env
file.
DB_CONNECTION_METHOD=my_custom_orm
DB_CONNECTION=mysql
DB_HOST=localhost
DB_NAME=my_database
DB_USERNAME=root
DB_PASSWORD=
Once the .env
file is configured, Lithe will automatically load these environment variables during the application startup. The Manager
class of Lithe will use these settings to initialize and establish the connection to the database as configured.
This process ensures that the configuration defined in the configure
method is used in conjunction with the environment variables, providing flexibility and efficient management of custom connections in your Lithe project.
Troubleshooting
If you encounter difficulties connecting to the database:
- Check if the settings and environment variables are correct.
- Look for possible syntax errors in your queries or SQL code.
Lithe offers a simple and flexible approach to managing connections with various databases and ORMs, facilitating the development of robust applications.