PHP code example of ray / doctrine-orm-module

1. Go to this page and download the library: Download ray/doctrine-orm-module library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ray / doctrine-orm-module example snippets


use Ray\Di\AbstractModule;
use Ray\DoctrineOrmModule\EntityManagerModule;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $entityDir = '/path/to/Entity/';

        $params = [
            'driver'   => 'pdo_pgsql',
            'user'     => 'username',
            'password' => 'password',
            'host'     => '127.0.0.1'
            'port'     => '5432',
            'dbname'   => 'myapp_db'
        ];

        $this->install(new EntityManagerModule($params, $entityDir));

        //// OR ////

        $params = [
            'url' => 'postgresql://username:[email protected]:5432/myapp_db'
        ];

        $this->install(new EntityManagerModule($params, $entityDir));
    }
}

use Ray\Di\AbstractModule;
use Ray\DoctrineOrmModule\EntityManagerModule;
use Ray\DoctrineOrmModule\TransactionalModule;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new EntityManagerModule($params, $entityDir));

        $this->install(new TransactionalModule); // <--
    }
}

use Ray\DoctrineOrmModule\Annotation\Transactional;
use Ray\DoctrineOrmModule\EntityManagerInject;

/**
 * @Transactional
 */
class UserService
{
    use EntityManagerInject;

    public function foo()
    {
        // transaction is active
        $this->entityManager->...;
    }

    public function bar()
    {
        // transaction is active
        $this->entityManager->...;
    }
}

use Ray\DoctrineOrmModule\Annotation\Transactional;
use Ray\DoctrineOrmModule\EntityManagerInject;

class UserService
{
    use EntityManagerInject;

    /**
     * @Transactional
     */
    public function foo()
    {
        // transaction is active
        $this->entityManager->...;
    }

    public function bar()
    {
        // transaction is not active
        $this->entityManager->...;
    }
}

use Ray\Di\AbstractModule;
use Ray\DoctrineOrmModule\Annotation\ProxyDir;
use Ray\DoctrineOrmModule\EntityManagerModule;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->bind()->annotatedWith(ProxyDir::class)->toInstance('/path/to/proxy'); // <--

        $this->install(new EntityManagerModule($params, $entityDir));
    }
}

use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Ray\Di\AbstractModule;
use Ray\DoctrineOrmModule\EntityManagerModule;
use Ray\DoctrineOrmModule\SqlLoggerModule;
use Ray\DoctrineOrmModule\TransactionalModule;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new EntityManagerModule($params, $entityDir));
        $this->install(new TransactionalModule);

        $this->bind(LoggerInterface::class)->toInstance(new Logger('myapp')); // <--
        $this->install(new SqlLoggerModule); // <--
    }
}
bash
$ php docs/demo/run.php
// It works!