Skip to content

Install from Scratch

Barry O'Donovan edited this page Feb 7, 2015 · 1 revision

I have composer installed:

$ composer --version
Composer version 1.0-dev (07c644ac229a21df80180598d8bb9aaba232eecb) 2015-02-03 12:51:10

Create a Laravel4 project:

composer create-project laravel/laravel test-d2b-l5

Edit composer.json:

"require": {
    ....,
    "opensolutions/doctrine2bridge-l5": "2.4.*"
},
...

Then run:

composer update

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

'Doctrine2Bridge\Doctrine2CacheBridgeServiceProvider',
'Doctrine2Bridge\Doctrine2BridgeServiceProvider',

You'll need to publish and edit the configuration files:

./artisan vendor:publish --provider "Doctrine2Bridge\Doctrine2CacheBridgeServiceProvider"
./artisan vendor:publish --provider "Doctrine2Bridge\Doctrine2BridgeServiceProvider"

You now need to set database and cache options:

  • the default cache in config/cache.php of file is fine.
  • the default namespace in config/d2bcache.php is also fine.
  • all the defaults in config/d2bdoctrine.php are fine.
  • we only need a MySQL database to be configured in config/database.php:

Assuming you have configured a test database in MySQL as follows:

CREATE DATABASE IF NOT EXISTS `d2btest`;
GRANT ALL ON d2btest.* TO `d2btest`@`127.0.0.1` IDENTIFIED BY 'd2btest';
FLUSH PRIVILEGES;

Edit config/database.php to match:

return [
    // ...
    'default' => 'mysql',

    // ...
    'connections' => [
        // ...
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'd2btest',
            'username'  => 'd2btest', 
            'password'  => 'd2btest',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
    ]
    // ...
];

Testing Doctrine2 Cache

Spin up Laravel's tinker environment - an interactive interpreter with Laravel's environemtn loaded:

./artisan tinker

We'll then show the cache working by:

  • loading a value that does not exist (returns false)
  • setting that value
  • attempt to load it again from the cache

Here's what you should type and expect:

$ ./artisan tinker
Psy Shell v0.3.3 (PHP 5.6.5 — cli) by Justin Hileman
>>> var_dump( D2Cache::fetch( 'abcdef' ) );
bool(false)
=> null
>>> var_dump( D2Cache::save( 'abcdef', 'qwerty' ) );
bool(true)
=> null
>>> var_dump( D2Cache::fetch( 'abcdef' ) );
string(6) "qwerty"
=> null
>>>

Testing Doctrine2 Entity Manager

Create a sample XML schema:

mkdir -p database/xml
cat >database/xml/Entities.SampleEntity.dcm.xml <<ENDXML
<?xml version="1.0"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="Entities\SampleEntity" repository-class="Repositories\Sample">
        <id name="id" type="integer">
            <generator strategy="AUTO"/>
        </id>
        <field name="name" type="string" length="255" nullable="true"/>
    </entity>
</doctrine-mapping>
ENDXML

Now create the entities, proxies and repositories (NB: you may need to do this twice to update the optimized class):

./artisan d2b:generate:entities
composer dump-autoload
./artisan d2b:generate:proxies
composer dump-autoload
./artisan d2b:generate:repositories
./artisan optimize

You can now (drop) and create the database with:

./artisan d2b:schema:drop --commit
./artisan d2b:schema:create --commit

Now, spin up tinker again and create some objects:

for( $i = 0; $i < 50; $i++ ) {
    $se = new Entities\SampleEntity;
    $se->setName( rand( 0, 100 ) );
    D2EM::persist( $se );
}
D2EM::flush();
echo count( D2R::r( 'SampleEntity' )->findAll() ) . ' objects created.';

Here's an example session:

>>> for( $i = 0; $i < 50; $i++ ) {
...     $se = new Entities\SampleEntity;
...     $se->setName( rand( 0, 100 ) );
...     D2EM::persist( $se );
... }
=> null
>>> D2EM::flush();
=> null
>>> echo count( D2R::r( 'SampleEntity' )->findAll() ) . ' objects created.';
50 objects created.⏎
=> null
Clone this wiki locally