PHP code example of thomasvargiu / laminas-manager-registry

1. Go to this page and download the library: Download thomasvargiu/laminas-manager-registry 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/ */

    

thomasvargiu / laminas-manager-registry example snippets


use TMV\Laminas\ManagerRegistry\EntityRepositoryFactory;
use TMV\Laminas\ManagerRegistry\ManagerRegistryConfigFactory;

return [
    ManagerRegistryConfigFactory::class => [
        'connections' => [
            'connection1' => 'doctrine.connection.orm_default',
            'connection2' => 'doctrine.connection.orm_another',
        ],
        'managers' => [
            'manager1' => 'doctrine.entitymanager.orm_default',
            'manager2' => 'doctrine.entitymanager.orm_another',
        ],
        'default_connection' => 'connection1', // optional, default to first connection
        'default_manager' => 'manager1', // optional, default to first manager
    ],
    'dependencies' => [
        'factories' => [
            // Create an EntityRepository service with a ManagerRegistry (see below)
            MyEntityRepository::class => new EntityRepositoryFactory(MyEntity::class),
        ],
    ],
];

use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;

/** @var ManagerRegistry $managerRegistry */
$managerRegistry = $container->get(ManagerRegistry::class);
/** @var EntityManagerInterface $entityManager */
$entityManager = $managerRegistry->getManager(/* <optional manager name> */);

if (! $entityManager->isOpen()) {
    /** @var EntityManagerInterface $entityManager */
    $entityManager = $managerRegistry->resetManager(/* <optional manager name> */);
}

use TMV\Laminas\ManagerRegistry\EntityRepository;

class MyRepository extends EntityRepository
{
    public function findByMyCriteria(string $value)
    {
        return $this->findBy(['my_criteria' => $value]) ;   
    }
    
    public function anotherMethod(): void
    {
        // get the doctrine EntityRepository, using the ManagerRegistry
        $doctrineRepository = $this->getRepository();
        // get the EntityManager, using the ManagerRegistry
        $entityManager = $this->getEntityManager();
    }
}

use TMV\Laminas\ManagerRegistry\EntityRepositoryFactory;
use TMV\Laminas\ManagerRegistry\ManagerRegistryConfigFactory;

return [
    'dependencies' => [
        'factories' => [
            MyRepository::class => new EntityRepositoryFactory(MyEntity::class /*, <optional manager name> */),
        ],
    ],
];