- Developers
-
-
-
Delivering purpose-built software solutions that build lasting client relations.
-
-
-
Web Development
- PHP
- Laravel
- WordPress
- Machine Learning
- UI/UX Engineers
-
-
-
Mobile Development
- Swift
- Android
- Java
- React Native
-
-
-
- Designers
-
-
-
Designing applications that reflect the values of your brand.
-
-
-
- About Us
- Blog
- Case Study
- Contact Us
The Object Relational Mapper (ORM) for PHP sits on top of a powerful DataBase Abstraction Layer (DBAL). One of its key features is the option to write database queries in a proprietary, object oriented, SQL dialect called Doctrine Query Language (DQL), inspired by Hibernates HQL. This provides developers with a powerful alternative to SQL that maintains flexibility without requiring unnecessary code duplication.
Laravel is a popular PHP web application framework that provides a simple and elegant syntax to create web applications. It comes with built-in support for database management through the use of Object Relational Mapping (ORM) libraries. Doctrine 2 ORM is a powerful library that provides a simple and easy-to-use interface for managing database interactions in PHP applications. In this article, we will explore how to set up Doctrine2 ORM with Laravel 5.
Installing Doctrine2 ORM
Before we start setting up Doctrine2 ORM with Laravel 5, we need to install the Doctrine 2 ORM library. We can install Doctrine2 ORM via Composer, which is a dependency management tool for PHP. Open the command prompt and navigate to the root directory of your Laravel 5 project, then run the following command:
composer require doctrine/orm
This command will install the Doctrine2 ORM library and its dependencies in your Laravel 5 project.
Configuring Doctrine2 ORM
After installing the Doctrine 2 ORM library, we need to configure it with Laravel 5. The configuration files are located in the config
directory of your Laravel 5 project. Open the database.php
file and add the following code to the connections
array:
'doctrine' => [
'driver' => 'pdo_mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'url' => env('DATABASE_URL'),
],
This code adds a new database connection named doctrine
to Laravel 5. We can use this connection to interact with the database using Doctrine2 ORM.
Next, open the app.php
file and add the following code to the providers
array:
Doctrine\ORM\Providers\Bindings\DoctrineServiceProvider::class,
This code registers the Doctrine2 ORM service provider with Laravel 5.
Creating Models with Doctrine2 ORM
Now that we have installed and configured Doctrine2 ORM with Laravel 5, we can start creating models to interact with the database. In Doctrine2 ORM, each model represents a table in the database. We can use the php artisan
command to create a new model. Open the command prompt and navigate to the root directory of your Laravel 5 project, then run the following command:
php artisan make:entity Product
This command will create a new Product
model in the app/Models
directory of your Laravel 5 project. Open the Product.php
file and add the following code:
namespace App\Models;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="products")
*/
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $name;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\Column(type="decimal", precision=10, scale=2)
*/
private $price;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
And you should be all set!