paolooo/laravel-doctrine

Doctrine 2 for Laravel 5 Artisans

0.4.0 2015-04-08 04:16 UTC

This package is not auto-updated.

Last update: 2024-11-09 17:03:54 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Build Status

Run all doctrine commands easily using $ php artisan doctrine <command> <options>

This service will grab your laravel's configuration (db and cache) and automatically apply it to doctrine2 configurate, no more hassle configuration needed. :)

  • Supports all Doctrine's command line.
  • Supports multiple DB connection.

Installation

$ composer require "paolooo/laravel-doctrine":"0.4.*@dev"

Open and edit config/app.php configuration file, and add the following service provider code to the $providers array.

        'Paolooo\LaravelDoctrine\LaravelDoctrineServiceProvider',

Edit .env file. Add the following doctrine config. For more information, of doctrine configuration, see, http://doctrine-orm.readthedocs.org/en/latest/reference/advanced-configuration.html.

# .env
...

DOCTRINE_PROXY_AUTOGENERATED=false
DOCTRINE_PROXY_NAMESPACE=Acme\Domain\Model\Proxy
DOCTRINE_PROXY_DIR=app/Domain/Model/Proxy
DOCTRINE_MAPPING_DIR=app/Domain/Model

# READ
READ_DB_DATABASE=cqrs_read_db
READ_DOCTRINE_PROXY_AUTOGENERATED=false
READ_DOCTRINE_PROXY_NAMESPACE=app\Query\Model\Proxy
READ_DOCTRINE_PROXY_DIR=app/Query/Model/Proxy
READ_DOCTRINE_MAPPING_DIR=app/Query/Model

This configuration is for testing environment. Edit phpunit.xml file.

# phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit ...>
    ....
    <php>
        ...
        <env name="DB_DRIVER" value="pdo_sqlite"/>
        <env name="DB_DATABASE" value="storage/tests/db.sqlite"/>
        <env name="READ_DB_DATABASE" value="storage/tests/db_read.sqlite"/>
    </php>
</phpunit>

Usage

<?php

# Create new EntityManager
$em = \App::make('Doctrine\ORM\EntityManager');

# Saving user entity to a 'default' database connection
# you can use `$em->on('default')` as well.
$em->getRepository('Paolooo\Acme\Domain\Entity\User')->persist($user);
$em->flush();

# Saving user entity to a 'read' database
$em->on('read')
    ->getRepository('Paolooo\Acme\Domain\Entity\User')
    ->persist($user);
$em->on('read')->flush();

Multiple Connection

$em = \App::make('Doctrine\ORM\EntityManager');
...
$em->on('read')->persist($user);
$em->on('read')->flush();

$em->on('eventStore')->persist($user);
$em->on('eventStore')->flush();

Running Doctrine Commands

Sample artisan for doctrine.

$ php artisan doctrine
$ php artisan doctrine help orm:schema-tool:create
$ php artisan doctrine orm:schema-tool:create
$ php artisan doctrine orm:schema-tool:create --dump-sql
$ php artisan doctrine orm:schema-tool:update
$ php artisan doctrine orm:schema-tool:drop

Example

See examples/ directory.

  • Sample config file, see .env and phpunit.xml files.
  • Sample User entity class, see Acme/Domain/Model/Entity/User.php file.
  • Sample ModelTestCase. This will get the setup doctrine for you. Sets $this->entityManager. Create and drop schema as well.

Learn doctrine here http://doctrine-orm.readthedocs.org/en/latest/tutorials/getting-started.html

TODO

  • Migration