Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Latest commit

 

History

History
325 lines (245 loc) · 8.7 KB

readme.md

File metadata and controls

325 lines (245 loc) · 8.7 KB

Laravel Doctrine

Join the chat at https://gitter.im/atrauzzi/laravel-doctrine Scrutinizer Code Quality Build Status SensioLabsInsight

This library is succeeded by laravel-doctrine/orm

Though this library is NOT abandoned a more feature-complete and up to date alternative is available at laravel-doctrine/orm. Check it out!

An ORM for a Framework for Web Artisans

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

Installation is the usual for Laravel packages.

Insert the following configs in your composer.json:

"minimum-stability": "dev",
"prefer-stable": true

In the packages section (require):

"atrauzzi/laravel-doctrine": "dev-master"

After that, just run a composer update

Add the service provider to your Laravel application in config/app.php. In the providers array add:

Atrauzzi\LaravelDoctrine\ServiceProvider::class,

If desired, add the following to your facades array in the same file:

'EntityManager' => Atrauzzi\LaravelDoctrine\Support\Facades\Doctrine::class,

You need to run this command publish package configuration.

php artisan vendor:publish --provider="Atrauzzi\LaravelDoctrine\ServiceProvider" --tag="config"

Usage

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');
EntityManager::persist($user);
EntityManager::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")
 */
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!

Doctrine Console

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

Authentication driver

This package allows you to customize the authentication driver using your own user model. In order to use doctrine authentication driver you need to keep in mind the following structure.

  • Having user model representing an authenticatable user into your application
  • Edit /config/doctrine.php config file to set authentication model and user provider
  • Edit /config/auth.php config file to set authentication driver.

Now, let's understand how this driver works.

User model

Your application must has a model implementing Illuminate\Contracts\Auth\Authenticatable. By default, this package comes with a Doctrine Authentication provider that works with a model using its email and password as unique valid credentials. The code below shows a valid user model:

namespace App\Models;

use Illuminate\Contracts\Auth\Authenticatable;

/**
 * Class User
 * @entity
 * @table(name="users")
 */
class User implements Authenticatable {

    /**
     * @var int
     * @column(type="integer", name="id_user")
     * @generatedValue(strategy="AUTO")
     * @id
     */
    protected $id;

    /**
     * @var string
     * @column(type="string", unique=true)
     */
    protected $email;

    /**
     * @var string
     * @column(type="string")
     */
    protected $password;

    /**
     * @var string
     * @column(type="string")
     */
    protected $token;

    public function getEmail()
    {
        return $this->email;
    }

    public function getPassword()
    {
        return $this->password;
    }


    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->id;
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->token;
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->token = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'remember_me_token';
    }
}

It is important to know that laravel needs that our model accomplishes with some rules provided by this interface:

  • getAuthIdentifier()
  • getRememberToken()
  • setRememberToken($value)
  • getRememberTokenName()
doctrine.php

Once you have created a valid user model, you are able to specify it in doctrine config file as below:

'auth' => [
    'authenticator' => 'Atrauzzi\LaravelDoctrine\DoctrineAuthenticator',
    'model' => 'App\Models\User',
]
  • If you want to base your authentication system by email and password you can use the default doctrine authenticator.
  • If you need to implement your own doctrine authenticator then set authenticator key by passing the classname.
  • If you want to use the native laravel auth driver, then set authenticator key a null value or just comment it.
auth.php

Finally, to set doctrine driver as default authentication system you need to set the value as doctrine.auth:

'driver' => 'doctrine.auth',

License

The Laravel framework is open-sourced software license under the MIT license

This project is too to ensure maximum compatibility.

Meta

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!