PHP code example of setono / doctrine-object-manager-trait

1. Go to this page and download the library: Download setono/doctrine-object-manager-trait 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/ */

    

setono / doctrine-object-manager-trait example snippets



use Doctrine\Persistence\ManagerRegistry;
use Setono\DoctrineObjectManagerTrait\ORM\ORMManagerTrait;

final class YourClass
{
    use ORMManagerTrait;
    
    public function __construct(ManagerRegistry $managerRegistry)
    {
        $this->managerRegistry = $managerRegistry;
    }
    
    public function someMethod(): void
    {
        /**
         * $entity is an entity managed by Doctrine or a class-string representing an entity managed by Doctrine
         */
        $entity = ;
        
        /**
         * Because we used the ORMManagerTrait above the getManager method will return an EntityManagerInterface
         * 
         * @var \Doctrine\ORM\EntityManagerInterface $manager 
         */
        $manager = $this->getManager($entity);
        
        $manager->persist($entity);
        $manager->flush();
    }
}