本文整理汇总了PHP中Doctrine\ORM\EntityManagerInterface::getConnection方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityManagerInterface::getConnection方法的具体用法?PHP EntityManagerInterface::getConnection怎么用?PHP EntityManagerInterface::getConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ORM\EntityManagerInterface
的用法示例。
在下文中一共展示了EntityManagerInterface::getConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getConnection
/**
* @return Connection
*/
public function getConnection()
{
/** @var Connection $con */
$con = $this->em->getConnection();
$con = $this->pingIt($con);
return $con;
}
示例2: urlToShortCode
/**
* Creates and persists a unique shortcode generated for provided url
*
* @param UriInterface $url
* @param string[] $tags
* @return string
* @throws InvalidUrlException
* @throws RuntimeException
*/
public function urlToShortCode(UriInterface $url, array $tags = [])
{
// If the url already exists in the database, just return its short code
$shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy(['originalUrl' => $url]);
if (isset($shortUrl)) {
return $shortUrl->getShortCode();
}
// Check that the URL exists
$this->checkUrlExists($url);
// Transactionally insert the short url, then generate the short code and finally update the short code
try {
$this->em->beginTransaction();
// First, create the short URL with an empty short code
$shortUrl = new ShortUrl();
$shortUrl->setOriginalUrl($url);
$this->em->persist($shortUrl);
$this->em->flush();
// Generate the short code and persist it
$shortCode = $this->convertAutoincrementIdToShortCode($shortUrl->getId());
$shortUrl->setShortCode($shortCode)->setTags($this->tagNamesToEntities($this->em, $tags));
$this->em->flush();
$this->em->commit();
return $shortCode;
} catch (ORMException $e) {
if ($this->em->getConnection()->isTransactionActive()) {
$this->em->rollback();
$this->em->close();
}
throw new RuntimeException('An error occurred while persisting the short URL', -1, $e);
}
}
示例3: execute
public function execute()
{
$criteria = new Criteria(['owner' => $this->user], null, null, null);
$items = $this->basketRepository->findByCriteria($criteria);
$order = $this->orderRepository->findActive();
$connection = $this->entityManager->getConnection();
$connection->beginTransaction();
try {
$orderItems = [];
foreach ($items as $item) {
/** @var Basket $item */
$previousOrderItem = $this->orderItemRepository->findOneByCriteria(new Criteria(['owner' => $item->getOwner(), 'product' => $item->getProduct()]));
if ($previousOrderItem) {
/** @var OrderItem $orderItem */
$orderItem = $previousOrderItem;
$orderItem->increaseQuantityBy($item->getQuantity());
} else {
$orderItem = OrderItem::createFromBasket($item, $order);
}
$this->entityManager->persist($orderItem);
$this->entityManager->remove($item);
$orderItems[] = $orderItem;
}
$this->entityManager->flush();
$connection->commit();
} catch (\Exception $e) {
$connection->rollBack();
return $e->getMessage();
}
}
示例4: purgeDatabase
/**
* @BeforeScenario
*/
public function purgeDatabase()
{
$this->entityManager->getConnection()->getConfiguration()->setSQLLogger(null);
$purger = new ORMPurger($this->entityManager);
$purger->purge();
$this->entityManager->clear();
}
示例5: encode
/**
* @param Url $url
* @return Url|object
* @throws \Doctrine\DBAL\ConnectionException
* @throws \Exception
*/
public function encode(Url $url)
{
$this->em->beginTransaction();
try {
$urlRepository = $this->em->getRepository('Rz\\Bundle\\UrlShortenerBundle\\Entity\\Url');
$entity = $urlRepository->findOneBy(['url' => $url->getUrl()]);
if ($entity) {
/** @var Url $url */
$url = $entity;
} else {
$url->setNew(true);
$this->em->persist($url);
$this->em->flush();
$url->setCode($this->encoder->encode($url->getId()));
$params = ['code' => $url->getCode()];
if (!$url->isDefaultSequence()) {
$params['index'] = $url->getSequence();
}
$url->setShortUrl($this->router->generate(UrlShortenerBundle::URL_GO, $params, UrlGeneratorInterface::ABSOLUTE_URL));
$this->em->persist($url);
$this->em->flush();
}
$this->em->getConnection()->commit();
return $url;
} catch (\Exception $e) {
$this->em->getConnection()->rollBack();
throw $e;
}
}
示例6: purgeUsingCachedQueries
private function purgeUsingCachedQueries()
{
$this->entityManager->getConnection()->getConfiguration()->setSQLLogger(null);
foreach ($this->cachedQueries as $cachedQuery) {
$this->entityManager->getConnection()->executeUpdate($cachedQuery['sql'], $cachedQuery['params'], $cachedQuery['types']);
}
}
示例7: loadCountry
/**
* Load country data
*
* @param string $countryIso Country iso
*
* @return $this Self object
*/
public function loadCountry($countryIso)
{
$content = $this->locationLoaderAdapter->getSqlForCountry($countryIso);
$statement = $this->locationEntityManager->getConnection()->prepare($content);
$statement->execute();
return $this;
}
示例8: tearDown
protected function tearDown()
{
//Close & unsets
if (is_object($this->em)) {
$this->em->getConnection()->close();
$this->em->close();
}
unset($this->em);
unset($this->container);
unset($this->kern);
unset($this->client);
//Nettoyage des mocks
//http://kriswallsmith.net/post/18029585104/faster-phpunit
$refl = new \ReflectionObject($this);
foreach ($refl->getProperties() as $prop) {
if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_')) {
$prop->setAccessible(true);
$prop->setValue($this, null);
}
}
//Nettoyage du garbage
if (!gc_enabled()) {
gc_enable();
}
gc_collect_cycles();
//Parent
parent::tearDown();
}
示例9: executeSqlFile
/**
* Executes given sql file
*
* @param string $path
* @throws \Doctrine\DBAL\DBALException
* @throws \Exception
*/
private function executeSqlFile($path)
{
if (!file_exists($path)) {
throw new \Exception(sprintf('SQL file does not exist: %s', $path));
}
$databaseSchema = file_get_contents($path);
$this->entityManager->getConnection()->exec($databaseSchema);
}
示例10: purgeDatabase
/**
* @BeforeScenario
*/
public function purgeDatabase()
{
if (null === $this->entityManager) {
throw new \RuntimeException('Cannot purge database. Entity manager is not set');
}
$this->entityManager->getConnection()->getConfiguration()->setSQLLogger(null);
$purger = new ORMPurger($this->entityManager);
$purger->purge();
$this->entityManager->clear();
}
示例11: defer
/**
* Defer task.
*
* @param int $pid
* @param callable $deferrer
*
* @return $this
*/
public function defer($pid, $deferrer)
{
$connection = $this->entityManager->getConnection();
$this->entity->setPid($pid);
$this->entityManager->flush();
$connection->close();
$exitCode = $deferrer();
$connection->connect();
$this->entity->setExitCode($exitCode);
$this->entityManager->flush();
return $this;
}
示例12: __construct
/**
* @param EntityManager $em
* @param Cache $cache
* @param CacheHitsContainer $hitsContainer
*/
public function __construct(EntityManagerInterface $em, Cache $cache, CacheHitsContainer $hitsContainer)
{
$this->em = $em;
$this->connection = $em->getConnection();
$this->cache = $cache;
$this->hitsContainer = $hitsContainer;
}
示例13: __construct
/**
* @param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em)
{
$params = $em->getConnection()->getParams();
$dsn = "mysql:host={$params['host']};dbname={$params['dbname']}";
$this->pdo = new PDO($dsn, $params['user'], $params['password']);
$this->pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL);
}
示例14: getTargetPlatform
/**
* @return Platforms\AbstractPlatform
*/
private function getTargetPlatform()
{
if (!$this->targetPlatform) {
$this->targetPlatform = $this->em->getConnection()->getDatabasePlatform();
}
return $this->targetPlatform;
}
示例15: getUpdateSchemaSql
/**
* Gets the sequence of SQL statements that need to be performed in order
* to bring the given class mappings in-synch with the relational schema.
* If $saveMode is set to true the command is executed in the Database,
* else SQL is returned.
*
* @param array $classes The classes to consider.
* @param boolean $saveMode True for writing to DB, false for SQL string.
*
* @return array The sequence of SQL statements.
*/
public function getUpdateSchemaSql(array $classes, $saveMode = false)
{
$sm = $this->em->getConnection()->getSchemaManager();
$fromSchema = $sm->createSchema();
$toSchema = $this->getSchemaFromMetadata($classes);
foreach ($toSchema->getTables() as $table) {
foreach ($table->getColumns() as $column) {
if ($column->getAutoincrement()) {
continue;
}
if (!preg_match('/^(?P<column>(?P<table>.*)_id)$/', $column->getName(), $matches)) {
continue;
}
$referencedTable = 'oc_' . $matches['table'];
if (!$toSchema->hasTable($referencedTable)) {
continue;
}
if ($toSchema->getTable($referencedTable) === $table) {
continue;
}
$table->addForeignKeyConstraint($toSchema->getTable($referencedTable), [$matches['column']], [$matches['column']]);
}
}
$comparator = new Comparator();
$schemaDiff = $comparator->compare($fromSchema, $toSchema);
foreach ($schemaDiff->changedTables as $changedTable) {
$changedTable->changedColumns = [];
}
if ($saveMode) {
return $schemaDiff->toSaveSql($this->platform);
}
return $schemaDiff->toSql($this->platform);
}