Doctrine\ORM\EntityManager::create PHP Method

create() public static method

Factory method to create EntityManager instances.
public static create ( mixed $conn, Doctrine\ORM\Configuration $config, Doctrine\Common\EventManager $eventManager = null ) : EntityManager
$conn mixed An array with the connection parameters or an existing Connection instance.
$config Doctrine\ORM\Configuration The Configuration instance to use.
$eventManager Doctrine\Common\EventManager The EventManager instance to use.
return EntityManager The created EntityManager.
    public static function create($conn, Configuration $config, EventManager $eventManager = null)
    {
        if (!$config->getMetadataDriverImpl()) {
            throw ORMException::missingMappingDriverImpl();
        }

        if (is_array($conn)) {
            $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, ($eventManager ?: new EventManager()));
        } else if ($conn instanceof Connection) {
            if ($eventManager !== null && $conn->getEventManager() !== $eventManager) {
                 throw ORMException::mismatchedEventManager();
            }
        } else {
            throw new \InvalidArgumentException("Invalid argument: " . $conn);
        }

        return new EntityManager($conn, $config, $conn->getEventManager());
    }

Usage Example

 /**
  * Because the EntityManager gets closed when there's an error, it needs to
  * be created again
  *
  * @return EntityManager
  * @throws ORMException
  */
 protected function getEntityManager()
 {
     if (!$this->em) {
         $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
     }
     if (!$this->em->isOpen()) {
         $this->em = $this->em->create($this->em->getConnection(), $this->em->getConfiguration());
     }
     return $this->em;
 }
All Usage Examples Of Doctrine\ORM\EntityManager::create