Skip to content

Install from Scratch

Barry O'Donovan edited this page Jan 25, 2014 · 5 revisions

I have composer installed:

$ composer --version
Composer version b20021cc6aa113069e4223b78d0074a1fc7dd4e8 2014-01-14 16:22:09

Create a Laravel4 project:

composer create-project laravel/laravel test3

Edit composer.json:

"require": {
    // ....,
    "opensolutions/doctrine2bridge": "2.4.*"
},
// ....
"minimum-stability": "dev"

Then run:

composer update

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

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

You'll need to public and edit the configuration file:

./artisan config:publish opensolutions/doctrine2bridge

Start up your application via:

./artisan serve

And view it on http://localhost:8000/.

Testing Doctrine2 Cache

Add the following to the top of app/routes.php:

echo '<pre>';
var_dump( D2Cache::fetch( 'abcdef' ) );
var_dump( D2Cache::save( 'abcdef', 'qwerty' ) );
var_dump( D2Cache::fetch( 'abcdef' ) );
die();

Refresh / local http://localhost:8000/ and expect to see:

bool(false)
bool(true)
string(6) "qwerty"

Now remove this test code from app/routes.php.

Testing Doctrine2 Entity Manager

NB: Ensure you have removed the cache test code from app/routes.php.

Create an XML schema:

mkdir -p doctrine/schema
cat >doctrine/schema/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

Edit app/config/packages/opensolutions/doctrine2bridge/config.php and create your database parameters. For example:

// connection parameters
'connection' => array(
    'driver'   => 'pdo_mysql',
    'dbname'   => 'test',
    'user'     => 'test',
    'password' => '',
    'host'     => '127.0.0.1',
    'charset'  => 'utf8'
),

Ensuring you have also configured the database. E.g.:

MariaDB [(none)]> GRANT ALL ON test.* TO `test`@`127.0.0.1`;
MariaDB [(none)]> FLUSH PRIVILEGES;

Now create the entities, proxies and repositories:

./vendor/bin/doctrine2 orm:generate-entities app/models/
./vendor/bin/doctrine2 orm:generate-proxies
./vendor/bin/doctrine2 orm:generate-repositories app/models/

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

./vendor/bin/doctrine2 orm:schema-tool:drop --force
./vendor/bin/doctrine2 orm:schema-tool:create

Now, add the following to app/routes.php:

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