Laravel's Eloquent ORM is excellent for lightweight use, however there's little out there that can beat Doctrine when you need a more full-featured ORM.
This is an integration of Doctrine 2.x to Laravel as a composer package. Doctrine's EntityManager
instance is accessible through a facade named Doctrine
as well as via dependency injection.
Metadata is obtained via the annotation driver or a custom config driver that leverages a Laravel-like configuration syntax.
Installation is the usual for Laravel packages.
Insert the following in the packages section of your composer.json
file and run an update:
"atrauzzi/laravel-doctrine": "dev-master",
Add the service provider to your Laravel application in config/app.php
. In the providers
array add:
'Atrauzzi\LaravelDoctrine\ServiceProvider',
If desired, add the following to your facades
array in the same file:
'EntityManager' => 'Atrauzzi\LaravelDoctrine\Support\Facades\Doctrine',
You need to run this command publish package configuration.
php artisan vendor:publish --provider="Vendor\atrauzzi\LaravelDoctrine\src\ServiceProvider" --tag="config"
You can obtain the EntityManager
instance for your connection simply by using the Doctrine
facade:
Adapted from Doctrine's documentation:
<?php
$user = new User;
$user->setName('Mr.Right');
Doctrine::persist($user);
Doctrine::flush();
Sample Entity in Laravel 5:
namespace App\Lib\Domain\Entities;
use Doctrine\ORM\Mapping as ORM;
use Atrauzzi\LaravelDoctrine\Trait\Time;
/**
* @ORM\Entity
* @ORM\Table(name="Post")
* @ORM\HasLifecycleCallbacks()
*/
class Post
{
use Time;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $body;
public function __construct($input)
{
$this->setTitle($input['title']);
$this->setBody($input['body']);
}
public function getId()
{
return $this->id;
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title=$title;
}
public function setBody($body)
{
$this->body=$body;
}
public function getBody()
{
return $this->body;
}
}
It is recommended that you read through all of the ORM documentation. Try using Laravel's console to experiment and go through the tutorials.
Enjoy!
If you need to run ORM commands it is necessary a cli-config.php
file at root project folder having the following implementation:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Illuminate\Foundation\Application;
require __DIR__.'/bootstrap/autoload.php';
/** @var Application $app */
$app = require_once __DIR__.'/bootstrap/app.php';
/** @var Illuminate\Contracts\Http\Kernel $kernel */
$kernel = $app->make('Illuminate\Contracts\Console\Kernel');
$kernel->bootstrap();
$app->boot();
$entityManager = $app->make('Doctrine\ORM\EntityManager');
return ConsoleRunner::createHelperSet($entityManager);
For validate your schema, you can do:
$ vendor/bin/doctrine orm:validate-schema
The Laravel framework is open-sourced software license under the MIT license
This project is too to ensure maximum compatibility.
I'm interested in hearing feedback and suggestions about this package. Please feel free to submit a ticket at any time.
Visit laravel-doctrine:
laravel-doctrine is made by Alexander Trauzzi with help from all the people in contributors.md
!