本文整理匯總了PHP中Doctrine\ORM\EntityManager::create方法的典型用法代碼示例。如果您正苦於以下問題:PHP EntityManager::create方法的具體用法?PHP EntityManager::create怎麽用?PHP EntityManager::create使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Doctrine\ORM\EntityManager
的用法示例。
在下文中一共展示了EntityManager::create方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: getEntityManager
/**
* 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;
}
示例2: hydrate
public function hydrate(Jarvis $app)
{
$app['doctrine.cache'] = function () {
return new VoidCache();
};
$app['doctrine.annotation.driver'] = function () {
return new AnnotationDriver(new AnnotationReader());
};
$app['entyMgr'] = function (Jarvis $app) : EntityManagerInterface {
$settings = $app['doctrine.settings'];
$cache = $app['doctrine.cache'];
$config = Setup::createConfiguration($settings['debug'], $settings['proxies_dir'], $cache);
$driver = $app['doctrine.annotation.driver'];
if (isset($settings['entities_paths'])) {
$driver->addPaths((array) $settings['entities_paths']);
}
AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);
$config->setAutoGenerateProxyClasses($settings['debug']);
$config->setMetadataCacheImpl($cache);
$config->setResultCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$entyMgr = EntityManager::create($settings['dbal'], $config);
if (isset($app['doctrine.orm.entyMgr.decorator']) && is_string($fqcn = $app['doctrine.orm.entyMgr.decorator']) && is_subclass_of($fqcn, EntityManagerDecorator::class)) {
$entyMgr = new $fqcn($entyMgr);
}
$entyMgr->getEventManager()->addEventListener([Events::preRemove, Events::postRemove, Events::prePersist, Events::postPersist, Events::preUpdate, Events::postUpdate, Events::postLoad, Events::preFlush, Events::onFlush, Events::postFlush, Events::onClear], new EventListener($app));
$app->broadcast(DoctrineReadyEvent::READY_EVENT, new DoctrineReadyEvent($entyMgr));
return $entyMgr;
};
$app['db_conn'] = function ($app) {
$app['entyMgr']->getConnection();
};
$app->lock(['entyMgr', 'db_conn', 'doctrine.annotation.driver']);
}
示例3: getMockEntityManager
protected function getMockEntityManager()
{
$driver = $this->getMock('Doctrine\DBAL\Driver');
$driver->expects($this->once())
->method('getDatabasePlatform')
->will($this->returnValue($this->getMock('Doctrine\DBAL\Platforms\MySqlPlatform')));
$conn = $this->getMock('Doctrine\DBAL\Connection', array(), array(array(), $driver));
$conn->expects($this->once())
->method('getEventManager')
->will($this->returnValue($this->getMock('Doctrine\Common\EventManager')));
$config = $this->getMock('Doctrine\ORM\Configuration');
$config->expects($this->once())
->method('getProxyDir')
->will($this->returnValue('test'));
$config->expects($this->once())
->method('getProxyNamespace')
->will($this->returnValue('Proxies'));
$config->expects($this->once())
->method('getMetadataDriverImpl')
->will($this->returnValue($this->getMock('Doctrine\ORM\Mapping\Driver\DriverChain')));
$em = EntityManager::create($conn, $config);
return $em;
}
示例4: __construct
public function __construct()
{
try {
$conn = array("driver" => "pdo_mysql", "host" => "localhost", "port" => "3306", "user" => "root", "password" => "", "dbname" => "controle_gastos");
/*
var_dump(__DIR__);
var_dump(PP);
exit;
*/
$loader = new \Doctrine\Common\ClassLoader("Entities", __DIR__);
$loader->register();
$config = Setup::createAnnotationMetadataConfiguration(array("../../" . __DIR__ . "/app/models"), false);
$em = EntityManager::create($conn, $config);
$cmf = new DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em);
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
$driver = new DatabaseDriver($em->getConnection()->getSchemaManager());
$em->getConfiguration()->setMetadataDriverImpl($driver);
$metadata = $cmf->getAllMetadata();
$generator = new EntityGenerator();
$generator->setGenerateAnnotations(true);
$generator->setGenerateStubMethods(true);
$generator->setRegenerateEntityIfExists(true);
$generator->setUpdateEntityIfExists(true);
$generator->generate($metadata, "../../" . __DIR__ . "/app/models");
} catch (\Exception $e) {
throw $e;
}
}
示例5: _initDoctrineEntityManager
public function _initDoctrineEntityManager()
{
$this->bootstrap(array('classLoaders'));
$zendConfig = $this->getOptions();
// parameters required for connecting to the database.
// the required attributes are driver, host, user, password and dbname
$connectionParameters = $zendConfig['doctrine']['connectionParameters'];
// now initialize the configuration object
$configuration = new DoctrineConfiguration();
// the metadata cache is used to avoid parsing all mapping information every time
// the framework is initialized.
//$configuration->setMetadataCacheImpl($this->getResource('doctrineCache'));
//// for performance reasons, it is also recommended to use a result cache
//$configuration->setResultCacheImpl($this->getResource('doctrineCache'));
// if you set this option to true, Doctrine 2 will generate proxy classes for your entities
// on the fly. This has of course impact on the performance and should therefore be disabled
// in the production environment
$configuration->setAutoGenerateProxyClasses($zendConfig['doctrine']['autoGenerateProxyClasses']);
// the directory, where your proxy classes live
$configuration->setProxyDir($zendConfig['doctrine']['proxyPath']);
// the proxy classes' namespace
$configuration->setProxyNamespace($zendConfig['doctrine']['proxyNamespace']);
// the next option tells doctrine which description language we want to use for the mapping
// information
$configuration->setMetadataDriverImpl($configuration->newDefaultAnnotationDriver($zendConfig['doctrine']['entityPath']));
// next, we create an event manager
$eventManager = new DoctrineEventManager();
// now we have everything required to initialize the entity manager
$entityManager = DoctrineEntityManager::create($connectionParameters, $configuration, $eventManager);
Zend_Registry::set('em', $entityManager);
return $entityManager;
}
示例6: getEntityManager
/**
* @return EntityManager
* @throws ServiceNotRegisteredException
*/
public static function getEntityManager()
{
if (ServiceLocator::$entityManager === null) {
ServiceLocator::$entityManager = EntityManager::create(['driver' => Config::DB_DRIVER, 'host' => Config::DB_HOST, 'dbname' => Config::DB_NAME, 'user' => Config::DB_USER, 'password' => Config::DB_PASS], Setup::createAnnotationMetadataConfiguration([__DIR__ . '/../Models'], Config::DEV_MODE, null, null, false));
}
return ServiceLocator::checkAndReturn(ServiceLocator::$entityManager);
}
示例7: register
public function register()
{
$this->container['object_manager'] = function () {
$development = false;
if (defined('ENVIRONMENT') && ENVIRONMENT === 'development') {
$development = true;
}
$config = new Configuration();
if ($development) {
$cache = new ArrayCache();
} else {
$cache = new ApcCache();
}
$config->setMetadataCacheImpl($cache);
$mappings = $this->container->get('config')['orm']['mappings'];
$config->setMetadataDriverImpl(new XmlDriver($mappings));
$proxyDir = $this->container->get('config')['orm']['proxy_dir'];
$config->setProxyDir($proxyDir);
$config->setQueryCacheImpl($cache);
$config->setProxyNamespace('Jirro\\ORM\\Proxies');
if ($development) {
$config->setAutoGenerateProxyClasses(true);
} else {
$config->setAutoGenerateProxyClasses(false);
}
$dbConnection = $this->container->get('db_connection');
$objectManager = ObjectManager::create($dbConnection, $config);
return $objectManager;
};
$this->container->inflector('Jirro\\Component\\ORM\\ObjectManagerAwareInterface')->invokeMethod('setObjectManager', ['object_manager']);
}
示例8: createService
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('config');
// The parameters in Doctrine 2 and ZF2 are slightly different.
// Below is an example how we can reuse the db settings
$doctrineDbConfig = (array) $config['db'];
$doctrineDbConfig['driver'] = strtolower($doctrineDbConfig['driver']);
if (!isset($doctrineDbConfig['dbname'])) {
$doctrineDbConfig['dbname'] = $doctrineDbConfig['database'];
}
if (!isset($doctrineDbConfig['host'])) {
$doctrineDbConfig['host'] = $doctrineDbConfig['hostname'];
}
if (!isset($doctrineDbConfig['user'])) {
$doctrineDbConfig['user'] = $doctrineDbConfig['username'];
}
$doctrineConfig = Setup::createAnnotationMetadataConfiguration($config['doctrine']['entity_path'], true);
$entityManager = DoctrineEntityManager::create($doctrineDbConfig, $doctrineConfig);
if (isset($config['doctrine']['initializers'])) {
$eventManager = $entityManager->getEventManager();
foreach ($config['doctrine']['initializers'] as $initializer) {
$eventClass = new DoctrineEvent(new $initializer(), $serviceLocator);
$eventManager->addEventListener(\Doctrine\ORM\Events::postLoad, $eventClass);
}
}
if ($serviceLocator->has('doctrine-profiler')) {
$profiler = $serviceLocator->get('doctrine-profiler');
$entityManager->getConfiguration()->setSQLLogger($profiler);
}
return $entityManager;
}
示例9: entity_manager
/**
* Creates a new EntityManager instance based on the provided configuration.
*
* $factory = new Doctrine_EMFactory;
* $em = $factory->entity_manager();
*
* @param string $db_group the name of the Kohana database config group to get connection information from
*
* @return \Doctrine\ORM\EntityManager
*/
public function entity_manager($db_group = 'default')
{
$config = $this->config->load('doctrine');
// Ensure the composer autoloader is registered
require_once $config['composer_vendor_path'] . 'autoload.php';
// Create the Configuration class
$orm_config = new Configuration();
// Create the metadata driver
$driver = $this->create_annotation_driver();
$orm_config->setMetadataDriverImpl($driver);
// Configure the proxy directory and namespace
$orm_config->setProxyDir($config['proxy_dir']);
$orm_config->setProxyNamespace($config['proxy_namespace']);
if ($config->get('use_underscore_naming_strategy')) {
$naming_strategy = new UnderscoreNamingStrategy($config->get('case_underscore_naming_strategy'));
$orm_config->setNamingStrategy($naming_strategy);
}
// Configure environment-specific options
if ($this->environment === Kohana::DEVELOPMENT) {
$orm_config->setAutoGenerateProxyClasses(TRUE);
$cache = new ArrayCache();
} else {
$orm_config->setAutoGenerateProxyClasses(FALSE);
$cache = new ApcCache();
}
// Set the cache drivers
$orm_config->setMetadataCacheImpl($cache);
$orm_config->setQueryCacheImpl($cache);
// Create the Entity Manager with the database connection information
$em = EntityManager::create($this->get_connection_config($db_group), $orm_config);
$this->register_custom_types($config);
return $em;
}
示例10: createEntityManager
/**
* @return \Doctrine\ORM\EntityManager
*/
protected function createEntityManager()
{
// event manager used to create schema before tests
$eventManager = new EventManager();
$eventManager->addEventListener(array("preTestSetUp"), new SchemaSetupListener());
// doctrine xml configs and namespaces
$configPathList = array();
if (is_dir(__DIR__ . '/../Resources/config/doctrine')) {
$dir = __DIR__ . '/../Resources/config/doctrine';
$configPathList[] = $dir;
$prefixList[$dir] = 'Kitpages\\DataGridBundle\\Entities';
}
if (is_dir(__DIR__ . '/_doctrine/config')) {
$dir = __DIR__ . '/_doctrine/config';
$configPathList[] = $dir;
$prefixList[$dir] = 'Kitpages\\DataGridBundle\\Tests\\TestEntities';
}
// create drivers (that reads xml configs)
$driver = new \Symfony\Bridge\Doctrine\Mapping\Driver\XmlDriver($configPathList);
$driver->setNamespacePrefixes($prefixList);
// create config object
$config = new Configuration();
$config->setMetadataCacheImpl(new ArrayCache());
$config->setMetadataDriverImpl($driver);
$config->setProxyDir(__DIR__ . '/TestProxies');
$config->setProxyNamespace('Kitpages\\DataGridBundle\\Tests\\TestProxies');
$config->setAutoGenerateProxyClasses(true);
//$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
// create entity manager
$em = EntityManager::create(array('driver' => 'pdo_sqlite', 'path' => "/tmp/sqlite-test.db"), $config, $eventManager);
return $em;
}
示例11: loadDoctrineOrm
private function loadDoctrineOrm(Application $app)
{
$self = $this;
$app['db.orm.em'] = $app->share(function () use($self, $app) {
return EntityManager::create($app['db'], $app['db.orm.config']);
});
}
示例12: __construct
/**
* @param array $database
* @throws \Exception
*/
public function __construct($database = [])
{
$this->db = $database;
foreach ($this->db as $key => $db) {
$this->allDb[$key] = function () use($db) {
$db['dev'] = isset($db['dev']) && $db['dev'] ? true : false;
if (isset($db['db_url'])) {
$dbParams = array('url' => $db['db_url']);
} else {
if (!isset($db['driver']) || !isset($db['user']) || !isset($db['pass']) || !isset($db['host']) || !isset($db['db'])) {
throw new \Exception('Missing arguments for doctrine constructor');
}
$dbParams = array('driver' => $this->getDriver($db['driver']), 'user' => $db['user'], 'password' => $db['pass'], 'host' => $db['host'], 'dbname' => $db['db'], 'charset' => isset($db['charset']) ? $db['charset'] : 'utf8');
}
$evm = new EventManager();
if (isset($db['prefix'])) {
$tablePrefix = new TablePrefix($db['prefix']);
$evm->addEventListener(Events::loadClassMetadata, $tablePrefix);
}
$config = Setup::createAnnotationMetadataConfiguration($db['path'], $db['dev']);
if (!$db['dev']) {
$config->setQueryCacheImpl($db['cache']);
$config->setResultCacheImpl($db['cache']);
$config->setMetadataCacheImpl($db['cache']);
}
if (isset($db['functions']) && !empty($db['functions'])) {
$config->setCustomDatetimeFunctions($db['functions']['customDatetimeFunctions']);
$config->setCustomNumericFunctions($db['functions']['customNumericFunctions']);
$config->setCustomStringFunctions($db['functions']['customStringFunctions']);
}
return EntityManager::create($dbParams, $config, $evm);
};
}
}
示例13: __construct
public function __construct($setConfigFiles = true)
{
if ($setConfigFiles) {
$this->configFile = __DIR__ . '/../../../config.php';
$this->localConfigFile = __DIR__ . '/../../../config.local.php';
}
parent::__construct();
$isDevMode = false;
$cache = new \Doctrine\Common\Cache\FilesystemCache(__DIR__ . '/../../tmp');
$config = Setup::createConfiguration($isDevMode, __DIR__ . '/../../tmp', $cache);
$config->setProxyDir(__DIR__ . '/../../tmp');
$config->setProxyNamespace('MyProject\\Proxies');
$config->setAutoGenerateProxyClasses(true);
$paths = [__DIR__ . '/../Entity'];
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver(new AnnotationReader(), $paths);
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);
//$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
$conn = ['driver' => 'mysqli', 'host' => '127.0.0.1', 'user' => $this->databaseFactory->getUserName(), 'password' => $this->databaseFactory->getPassword(), 'dbname' => $this->databaseFactory->getDatabaseName()];
$this->entityManager = EntityManager::create($conn, $config);
$this->entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
\Doctrine\DBAL\Types\Type::addType('enum', StringType::class);
$this->entityManager->getConfiguration()->addCustomStringFunction('DATE', DateFunction::class);
$this->user = $this->entityManager->createQueryBuilder()->select('u')->from(Sources\Tests\Entity\User::class, 'u');
}
示例14: register
public function register(Application $app)
{
$dbConnection = array('driver' => 'pdo_mysql', 'host' => 'localhost', 'user' => 'root', 'password' => 'password', 'dbname' => 'icfs');
$config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/Model"), true);
$app['db.em'] = \Doctrine\ORM\EntityManager::create($dbConnection, $config);
$app->register(new \Silex\Provider\DoctrineServiceProvider(), array('db.options' => $dbConnection));
}
示例15: factory
/**
* @param array $settings
* @param bool $debug
* @return EntityManager
* @throws \Doctrine\ORM\ORMException
*/
public static function factory($settings, $debug = true)
{
if ($debug || !function_exists('apc_fetch')) {
$cache = new ArrayCache();
} else {
$cache = new ApcCache();
}
$dbSettings = $settings['doctrine'];
$config = new Configuration();
$config->setMetadataCacheImpl($cache);
// Do not use default Annotation driver
$driverImpl = new AnnotationDriver(new AnnotationReader(), $dbSettings['entities']);
// Allow all annotations
AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setProxyDir($dbSettings['proxy_path']);
$config->setProxyNamespace('Zaralab\\Doctrine\\Proxies');
if ($debug) {
$config->setAutoGenerateProxyClasses(true);
} else {
$config->setAutoGenerateProxyClasses(false);
}
$connectionOptions = $dbSettings['dbal'];
return EntityManager::create($connectionOptions, $config);
}