本文整理汇总了PHP中Doctrine\ORM\EntityManagerInterface::beginTransaction方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityManagerInterface::beginTransaction方法的具体用法?PHP EntityManagerInterface::beginTransaction怎么用?PHP EntityManagerInterface::beginTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ORM\EntityManagerInterface
的用法示例。
在下文中一共展示了EntityManagerInterface::beginTransaction方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: batchUpdate
/**
* {@inheritdoc}
*/
public function batchUpdate(MassActionInterface $massAction, IterableResultInterface $results, array $data)
{
$this->entityName = $massAction->getOptions()->offsetGet('entityName');
$this->fieldName = empty($data['mass_edit_field']) ? null : $data['mass_edit_field'];
if (empty($this->fieldName)) {
throw new \RuntimeException("Field name was not specified with option 'mass_edit_field'");
}
$this->identifierName = $this->doctrineHelper->getSingleEntityIdentifierFieldName($this->entityName);
$value = $data[$this->fieldName];
$selectedIds = [];
$entitiesCount = 0;
$iteration = 0;
$this->entityManager = $this->doctrineHelper->getEntityManager($this->entityName);
$this->entityManager->beginTransaction();
try {
set_time_limit(0);
foreach ($results as $result) {
/** @var $result ResultRecordInterface */
$selectedIds[] = $result->getValue($this->identifierName);
$iteration++;
if ($iteration % $this->batchSize == 0) {
$entitiesCount += $this->finishBatch($selectedIds, $value);
}
}
if ($iteration % $this->batchSize > 0) {
$entitiesCount += $this->finishBatch($selectedIds, $value);
}
$this->entityManager->commit();
} catch (\Exception $e) {
$this->entityManager->rollback();
throw $e;
}
return $entitiesCount;
}
示例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: 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;
}
}
示例4: execute
/**
* Executes the given command and optionally returns a value
*
* @param object $command
* @param callable $next
* @return mixed
* @throws Exception
*/
public function execute($command, callable $next)
{
$this->entityManager->beginTransaction();
try {
$returnValue = $next($command);
$this->entityManager->flush();
$this->entityManager->commit();
} catch (Exception $e) {
$this->entityManager->close();
$this->entityManager->rollback();
throw $e;
}
return $returnValue;
}
示例5: save
/**
* @inheritdoc
*/
public function save(MessageModel $message) : MessageModel
{
$this->em->beginTransaction();
try {
$this->em->persist($message);
$this->em->flush();
$this->em->commit();
return $message;
} catch (UniqueConstraintViolationException $ex) {
$this->em->rollBack();
throw new InvalidArgumentException('Title is already registered', 409, $ex);
} catch (Exception $ex) {
$this->em->rollBack();
throw new InvalidArgumentException($ex->getMessage(), 500, $ex);
}
}
示例6: getIterator
/**
* {@inheritDoc}
*/
public function getIterator()
{
$iteration = 0;
$resultSet = clone $this->resultSet;
$this->entityManager->beginTransaction();
try {
foreach ($resultSet as $key => $value) {
$iteration += 1;
if (!is_object($value)) {
(yield $key => $value);
$this->flushAndClearBatch($iteration);
continue;
}
(yield $key => $this->reFetchObject($value));
$this->flushAndClearBatch($iteration);
}
} catch (\Exception $exception) {
$this->entityManager->rollback();
throw $exception;
}
$this->flushAndClearEntityManager();
$this->entityManager->commit();
}
示例7:
function it_should_throw_an_exception_if_em_is_closed(EntityManagerInterface $entityManager)
{
$entityManager->isOpen()->willReturn(false);
$entityManager->beginTransaction()->shouldNotBeCalled();
$this->shouldThrow('\\RemiSan\\TransactionManager\\Exception\\BeginException')->duringBeginTransaction();
}
示例8: beginTransaction
/**
* {@inheritdoc}
*/
public function beginTransaction()
{
return $this->wrapped->beginTransaction();
}
示例9: beginTransaction
/**
* @return bool
*/
public function beginTransaction()
{
$this->em->beginTransaction();
return true;
}
示例10: begin
/**
* {@inheritDoc}
*/
public function begin($key = null, array $options = [])
{
$this->entityManager->beginTransaction();
}
示例11: beginTransaction
/**
* @return void
*/
public function beginTransaction()
{
$this->entityManager->beginTransaction();
}
示例12: execute
public function execute()
{
$this->entityManager->getConnection()->setTransactionIsolation(Connection::TRANSACTION_SERIALIZABLE);
$this->entityManager->beginTransaction();
try {
$payload = $this->getContent();
$ext = false;
echo "processing >> " . $payload['image_src'] . " >> for id >> " . $payload['page_id'] . " >> for ext >> " . $payload['image_ext'] . "\n";
try {
$this->httpClient->setUri($payload['image_src']);
$this->httpClient->getRequest()->setMethod('HEAD');
$response = $this->httpClient->send();
if ($response->getHeaders()->get('Content-Type') !== false) {
$ext = $this->get_extension($response->getHeaders()->get('Content-Type')->getFieldValue());
}
} catch (\Zend\Http\Exception\InvalidArgumentException $e) {
echo "Exception: while sending HEAD method >> " . $e->getMessage() . "\n";
}
echo "declared ext >> " . $payload['image_ext'] . " >> detected ext >> " . $ext . "\n";
if ($ext === false) {
echo "Not an image \n";
$pageEntity = $this->updatePending($payload['page_id']);
echo "processed >> " . $payload['image_src'] . " >> pendingCnt >> " . $pageEntity->getPendingImagesCnt() . " >> status >> " . $pageEntity->getStatus() . "\n";
$this->entityManager->flush();
$this->entityManager->commit();
return;
}
$this->httpClient->getRequest()->setMethod('GET');
$response = $this->httpClient->send();
if ($ext == 'svg') {
$xmlget = simplexml_load_string($response->getBody());
$xmlattributes = $xmlget->attributes();
$width = preg_replace('/[^0-9.]/', '', strtolower((string) $xmlattributes->width));
$height = preg_replace('/[^0-9.]/', '', strtolower((string) $xmlattributes->height));
$imageInfo = [$width, $height, 'mime' => $response->getHeaders()->get('Content-Type')->getFieldValue()];
$imageSize = mb_strlen($response->getBody(), '8bit');
} else {
$imageInfo = getimagesizefromstring($response->getBody());
$contentLength = $response->getHeaders()->get('Content-Length');
if ($contentLength === false) {
$imageSize = mb_strlen($response->getBody(), '8bit');
} else {
$imageSize = $contentLength->getFieldValue();
}
}
$url = $this->storageService->store($payload['image_ext'], $response->getBody());
$imageEntity = new Images();
$imageEntity->setContentType($imageInfo['mime']);
$imageEntity->setWidth($imageInfo[0]);
$imageEntity->setHeight($imageInfo[1]);
$imageEntity->setSize($imageSize);
$imageEntity->setLocalPath($url);
$imageEntity->setRemotePath($payload['image_src']);
$pageEntity = $this->updatePending($payload['page_id']);
$imageEntity->setPage($pageEntity);
$this->entityManager->persist($imageEntity);
echo "processed >> " . $payload['image_src'] . " >> pendingCnt >> " . $pageEntity->getPendingImagesCnt() . " >> status >> " . $pageEntity->getStatus() . "\n";
$this->entityManager->flush();
$this->entityManager->commit();
} catch (\Exception $e) {
$this->entityManager->rollback();
echo "Exception : >>>> " . $e->getMessage() . "\n";
throw new ReleasableException(array('priority' => 10, 'delay' => 15));
}
}