本文整理汇总了PHP中Doctrine\ORM\EntityManagerInterface::flush方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityManagerInterface::flush方法的具体用法?PHP EntityManagerInterface::flush怎么用?PHP EntityManagerInterface::flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ORM\EntityManagerInterface
的用法示例。
在下文中一共展示了EntityManagerInterface::flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
}
示例2: write
/**
* Writes the record down to the log of the implementing handler
*
* @param array $record
*
* @return void
*/
protected function write(array $record)
{
// Ensure the doctrine channel is ignored (unless its greater than a warning error), otherwise you will create an infinite loop, as doctrine like to log.. a lot..
if ('doctrine' == $record['channel']) {
if ((int) $record['level'] >= Logger::WARNING) {
error_log($record['message']);
}
return;
}
// Only log errors greater than a warning
//@todo - you could ideally add this into configuration variable
if ((int) $record['level'] >= Logger::INFO && (string) $record['channel'] == 'backtrace') {
try {
// Create entity and fill with data
$entity = new SystemLog();
$entity->setChannel($record['channel'])->setLevel($record['level'])->setLog($record['message'])->setFormattedMsg($record['formatted'])->setCreatedAt(new \DateTime());
$this->em->persist($entity);
$this->em->flush();
} catch (\Exception $e) {
// Fallback to just writing to php error logs if something really bad happens
error_log($record['message']);
error_log($e->getMessage());
}
}
}
示例3: persist
/**
* Persists and flushes all provided models to the database storage.
*
* @param ...$models
*/
protected function persist(...$models)
{
foreach ($models as $model) {
$this->entityManager->persist($model);
}
$this->entityManager->flush();
}
示例4: setUp
public function setUp()
{
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->em->persist(Argument::any())->willReturn(null);
$this->em->flush()->willReturn(null);
$this->service = new ShortUrlService($this->em->reveal());
}
示例5: delete
public function delete(BaseEntityInterface $entity)
{
$this->entityManager->remove($entity);
if ($this->autoFlush) {
$this->entityManager->flush();
}
}
示例6: 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);
}
}
示例7: persist
/**
* Persists an entity via the EntityManager
*
* @param mixed $built A built entity
*/
public function persist($built)
{
$this->entityManager->persist($built);
if ($this->flushAfterPersist) {
$this->entityManager->flush();
}
}
示例8: updateEntity
/**
* @param $configKey
* @param $objectId
* @param $value
* @param null|string $dataField
* @throws ContentEditableException
*/
public function updateEntity($configKey, $objectId, $value, $dataField = null)
{
if (!array_key_exists($configKey, $this->configuration['configurations'])) {
throw new ContentEditableException('Missing configuration "' . $configKey . '"');
}
$config = $this->configuration['configurations'][$configKey];
if ($dataField == null) {
if (!array_key_exists('data_field', $config)) {
throw new ContentEditableException('Missing data_field');
}
$dataField = $config['data_field'];
}
$idField = 'id';
if (array_key_exists('id_field', $config)) {
$idField = $config['id_field'];
}
$repository = $this->entityManager->getRepository($config['repository_class']);
$entity = $repository->findOneBy([$idField => $objectId]);
$setter = 'set' . ucfirst($dataField);
if (!method_exists($entity, $setter)) {
throw new ContentEditableException('No setter "' . $setter . '" found for "' . get_class($entity) . '"');
}
$entity->{$setter}(trim($value));
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
示例9: saveAll
/**
* @param array|Student[] $students
*/
public function saveAll(array $students)
{
foreach ($students as $student) {
$this->em->persist($student);
}
$this->em->flush();
}
示例10: saveList
/**
* @param MailingList $list
*/
public function saveList(MailingList $list)
{
if (!$list->getId()) {
$this->em->persist($list);
}
$this->em->flush($list);
}
示例11: createLink
/**
* @param $type
* @param $id
* @param $loader
* @return Link
*/
protected function createLink($type, $id, $loader)
{
$link = new Link($type, $id, $loader);
$this->entityManager->persist($link);
$this->entityManager->flush($link);
return $link;
}
示例12: __invoke
public function __invoke()
{
if (null === ($renderer = $this->_renderer)) {
return '';
}
if (null === ($page = $renderer->getCurrentPage())) {
return '';
}
$metadata = $page->getMetadata();
if (null === $metadata || $metadata->count() === 0) {
// @todo gvf
$metadata = new MetaDataBag($this->metadataConfig, $page);
$page->setMetaData($metadata);
if ($this->entityManager->contains($page)) {
$this->entityManager->flush($page);
}
}
$result = '';
foreach ($metadata as $meta) {
if (0 < $meta->count() && 'title' !== $meta->getName()) {
$result .= '<meta ';
foreach ($meta as $attribute => $value) {
if (false !== strpos($meta->getName(), 'keyword') && 'content' === $attribute) {
$keywords = explode(',', $value);
foreach ($this->getKeywordObjects($keywords) as $object) {
$value = trim(str_replace($object->getUid(), $object->getKeyWord(), $value), ',');
}
}
$result .= $attribute . '="' . html_entity_decode($value, ENT_COMPAT, 'UTF-8') . '" ';
}
$result .= '/>' . PHP_EOL;
}
}
return $result;
}
示例13: 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();
}
}
示例14: notify
public function notify(UrlEvent $event)
{
if (!$this->isEnabled() || !$event->getUrl() || $event->getType() !== Shortener::NOTIFY_TYPE_REDIRECT) {
return false;
}
$additional = $event->getAdditional();
$ip = !empty($additional['ip']) ? $additional['ip'] : null;
if (!$ip) {
return false;
}
$urlRepository = $this->em->getRepository('Rz\\Bundle\\UrlShortenerBundle\\Entity\\Url');
$urlStatRepository = $this->em->getRepository('Rz\\Bundle\\UrlShortenerBundle\\Entity\\UrlStat');
/** @var Url $url */
$url = $urlRepository->find($event->getUrl()->getId());
$url->incrementRedirectCount();
$url->setLastRedirectOn(new \DateTime());
$urlStat = $urlStatRepository->findOneBy(['Url' => $url, 'ip' => $ip]);
if (!$urlStat) {
$url->incrementUniqueRedirectCount();
$urlStat = new UrlStat();
$urlStat->setUrl($url);
$urlStat->setCreated(new \DateTime());
$urlStat->setIp($ip);
if (!empty($additional['user-agent'])) {
$urlStat->setUserAgent($additional['user-agent']);
}
$this->em->persist($urlStat);
}
$this->em->persist($url);
$this->em->flush();
return true;
}
示例15: updateLastLogin
/**
* Update the users last login.
*
* @param UserInterface $user
*/
protected function updateLastLogin($user)
{
if ($user instanceof BaseUser) {
$user->setLastLogin(new \DateTime());
$this->entityManager->flush();
}
}