Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use silex with Doctrine orm EntityManager?

Am new to the Silex framework. And I would like to do simple SQL DB connection using doctrine entity manager. Kindly give some simple examples.

Thanks in Advance,
SK

like image 770
KSK Avatar asked Apr 16 '13 05:04

KSK


2 Answers

Here is a working example of all the following setup below. There is also a YouTube tutorial explaining that setup. Or if you just want something that works right now try: this.

Otherwise the following is an attempt to explain one way to setup and "use silex with Doctrine orm EntityManager":

Install

Add this line into composer.json:

"dflydev/doctrine-orm-service-provider": "1.0.6"

From command line run:

~$ composer update dflydev/doctrine-orm-service-provider

Register

Register the service provider:

$app->register(new Silex\Provider\DoctrineServiceProvider());
$app->register(new Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider());

Configure

Configure your database and entity mappings. Your paths may be different. A better and more detailed explaination can be found in the Doctrine ORM Service Provider README:

$app['db.options'] = array(
    'driver'   => 'pdo_mysql',
    'charset'  => 'utf8',
    'host'     => '127.0.0.1',
    'dbname'   => '',
    'user'     => '',
    'password' => '',
);

$app['orm.proxies_dir'] = __DIR__.'/../cache/doctrine/proxies';
$app['orm.default_cache'] = 'array';
$app['orm.em.options'] = array(
    'mappings' => array(
        array(
            'type' => 'annotation',
            'path' => __DIR__.'/../../src',
            'namespace' => 'My\\Namespace\\To\\Entity',
        ),
    ),
);

Likely you will want to setup cli-config.php for more info read the Doctrine Configuration Documentation:

<?php
    // http://docs.doctrine-project.org/en/latest/reference/configuration.html
    require __DIR__.'/vendor/autoload.php';
    require __DIR__.'/path/to/app/config.php';
    $newDefaultAnnotationDrivers = array(
        __DIR__."/src/MyNamespace",
    );
    $config = new \Doctrine\ORM\Configuration();
    $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ApcCache);
    $driverImpl = $config->newDefaultAnnotationDriver($newDefaultAnnotationDrivers);
    $config->setMetadataDriverImpl($driverImpl);
    $config->setProxyDir($app['orm.proxies_dir']);
    $config->setProxyNamespace('Proxies');
    $em = \Doctrine\ORM\EntityManager::create($app['db.options'], $config);
    $helpers = new Symfony\Component\Console\Helper\HelperSet(array(
        'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
        'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em),
    ));

Take it for a spin

At this point you should be able to dump some schema from the command line assuming you have annotated entities in $newDefaultAnnotationDrivers path:

~$ php vendor/bin/doctrine orm:schema-tool:update --dump-sql

With the above setup in place you should also be able to use $app['orm.em'] from a controller method:

$app->match('/form', function (Request $request) use ($app) {

    $em = $app['orm.em'];

    $entity = new \My\Namespace\To\Entity\Form();

    $form = $app['form.factory']->create(new \My\Namespace\To\Form\FormType(), $entity);

    $form->handleRequest($request);

    if ($form->isValid()) {
        $em->persist($entity);
        $em->flush();
    }

    return new Response($app['twig']->render('form.html.twig', array(
        'form' => $form->createView(),
    )));

})->bind('form');
like image 53
ooXei1sh Avatar answered Oct 05 '22 00:10

ooXei1sh


There isn't an official Doctrine ORM service provider for Silex only the DBAL, but there are several third party service providers you can try.
I stared using dflydev-doctrine-orm-service-provider which has the most starts in github and looks nice.

like image 40
olanod Avatar answered Oct 05 '22 00:10

olanod