当前位置: 首页>>代码示例>>PHP>>正文


PHP EntityManager::safePersist方法代码示例

本文整理汇总了PHP中Kdyby\Doctrine\EntityManager::safePersist方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityManager::safePersist方法的具体用法?PHP EntityManager::safePersist怎么用?PHP EntityManager::safePersist使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Kdyby\Doctrine\EntityManager的用法示例。


在下文中一共展示了EntityManager::safePersist方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: create

 /**
  * @param Url $url
  * @return Url
  * @throws UrlAlreadyExistsException
  */
 private function create(Url $url)
 {
     $url = $this->em->safePersist($url);
     if ($url === false) {
         throw new UrlAlreadyExistsException();
     }
     return $url;
 }
开发者ID:blitzik,项目名称:CMS,代码行数:13,代码来源:UrlPersister.php

示例2: execute

 /**
  * Executes the current command.
  *
  * This method is not abstract because you can use this class
  * as a concrete class. In this case, instead of defining the
  * execute() method, you set the code to execute by passing
  * a Closure to the setCode() method.
  *
  * @param InputInterface $input An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return null|int null or 0 if everything went fine, or an error code
  *
  * @throws LogicException When this abstract method is not implemented
  *
  * @see setCode()
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /** @var QuestionHelper $helper */
     $helper = $this->getHelper('question');
     do {
         if (!isset($username)) {
             $username = $this->askUsername($helper, $input, $output);
         }
         if (!isset($password)) {
             $password = $this->askPassword($helper, $input, $output);
         }
         if (!isset($email)) {
             $email = $this->askEmail($helper, $input, $output);
         }
         $output->writeln(sprintf('Summary:
                 username: %s
                 password: %s
                 E-mail: %s', $username, $password, $email));
         // CONFIRMATION
         $question = new ConfirmationQuestion('Do you want to create this new User? ', true);
         if (!$helper->ask($input, $output, $question)) {
             $output->writeln('New user insertion has been CANCELED!');
             return;
         }
         try {
             $user = new User($username, $email, $password);
             $new_user = $this->em->safePersist($user);
             if ($new_user === false) {
                 $uname = $this->userRepository->findOneBy(['username' => $username]);
                 if ($uname !== null) {
                     $output->writeln(sprintf('User with username "%s" already exists', $username));
                     $username = null;
                 }
                 $umail = $this->userRepository->findOneBy(['email' => $email]);
                 if ($umail !== null) {
                     $output->writeln(sprintf('User with email "%s" already exists', $email));
                     $email = null;
                 }
                 unset($user);
                 continue;
             }
             $output->writeln('Your new User has been SUCCESSFULLY created!');
             $continue = new ConfirmationQuestion('Would you like to create another one?', true);
             if ($helper->ask($input, $output, $continue)) {
                 $username = null;
                 $password = null;
                 $email = null;
                 continue;
             }
             return 0;
         } catch (\Exception $e) {
             $output->writeLn("That's bad. An Error occurred: <error>{$e->getMessage()}</error>");
             return 1;
         }
     } while (true);
 }
开发者ID:blitzik,项目名称:CMS,代码行数:73,代码来源:NewUserCommand.php

示例3: execute

 /**
  * Executes the current command.
  *
  * This method is not abstract because you can use this class
  * as a concrete class. In this case, instead of defining the
  * execute() method, you set the code to execute by passing
  * a Closure to the setCode() method.
  *
  * @param InputInterface $input An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return null|int null or 0 if everything went fine, or an error code
  *
  * @throws LogicException When this abstract method is not implemented
  *
  * @see setCode()
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /** @var QuestionHelper $helper */
     $helper = $this->getHelper('question');
     do {
         if (!isset($urlPath)) {
             $urlPath = $this->askUrlPath($helper, $input, $output);
         }
         if (!isset($presenter)) {
             $presenter = $this->askPresenter($helper, $input, $output);
         }
         if (!isset($internalID)) {
             $internalID = null;
             $internalIDConfirmation = new ConfirmationQuestion('Would you like to set Internal ID?', true);
             if ($helper->ask($input, $output, $internalIDConfirmation)) {
                 $internalID = $this->askInternalID($helper, $input, $output);
             }
         }
         $output->writeln(sprintf('Summary:
                  Url path: %s
                  Presenter: %s
                  internal ID: %s', $urlPath, $presenter, $internalID === null ? 'null' : $internalID));
         $anotherUrl = new ConfirmationQuestion('Do you want to save your Url?', true);
         if (!$helper->ask($input, $output, $anotherUrl)) {
             return;
         }
         try {
             $url = UrlGenerator::create($urlPath, $presenter, null, $internalID);
             $url = $this->em->safePersist($url);
             if ($url === false) {
                 $output->writeln(sprintf('Somebody has recently created an Url with path "%s".', $urlPath));
                 $changeQuestion = new ConfirmationQuestion('Do you want to change that Url path?', true);
                 if ($helper->ask($input, $output, $changeQuestion)) {
                     $urlPath = null;
                     continue;
                 }
                 $output->writeln(sprintf('Your Url couldn\'t have been saved because url with path "%s" already exists.', $urlPath));
                 return 1;
             }
             $output->writeln('New Url has been SUCCESSFULLY created!');
             $continueQuestion = new ConfirmationQuestion('Would you like to create another one?', true);
             if ($helper->ask($input, $output, $continueQuestion)) {
                 $urlPath = null;
                 $presenter = null;
                 $internalID = null;
                 continue;
             }
             return 0;
         } catch (\Exception $e) {
             $output->writeLn("That's bad. An Error occurred: <error>{$e->getMessage()}</error>");
             return -1;
         }
     } while (true);
 }
开发者ID:blitzik,项目名称:CMS,代码行数:71,代码来源:NewUrlCommand.php

示例4: create

 /**
  * @param array $values
  * @param Tag|null $tag
  * @return Tag
  * @throws TagNameAlreadyExistsException
  * @throws UrlAlreadyExistsException
  * @throws \Doctrine\DBAL\DBALException
  * @throws \Exception
  */
 private function create(array $values, Tag $tag = null)
 {
     $this->em->beginTransaction();
     if ($tag === null) {
         $tag = new Tag($values['name'], $values['color']);
     }
     $this->fillTag($values, $tag);
     /** @var Tag $tag */
     $tag = $this->em->safePersist($tag);
     if ($tag === false) {
         throw new TagNameAlreadyExistsException();
     }
     $this->createUrl($tag);
     $this->em->commit();
     return $tag;
 }
开发者ID:blitzik,项目名称:CMS,代码行数:25,代码来源:TagPersister.php

示例5: createNewPage

 /**
  * @param array $values
  * @param Page|null $page
  * @return Page
  * @throws UrlAlreadyExistsException
  * @throws LocaleNotFoundException
  * @throws PageTitleAlreadyExistsException
  * @throws PageIntroHtmlLengthException
  */
 private function createNewPage(array $values, Page $page = null)
 {
     $this->em->beginTransaction();
     $url = $this->establishPageUrl($values['title'], $values['url']);
     $url = $this->urlFacade->saveUrl($url);
     // still needs internalID to be set! (next in code)
     $locale = $this->localeFacade->getByName($values['lang']);
     if ($locale === null) {
         throw new LocaleNotFoundException();
     }
     if ($page === null) {
         $page = new Page($values['title'], $values['intro'], $values['text'], $url, $values['author'], $locale);
     }
     $this->fillPageEntity($values, $page);
     /** @var Page $page */
     $page = $this->em->safePersist($page);
     if ($page === false) {
         throw new PageTitleAlreadyExistsException();
     }
     $url->setInternalId($page->getId());
     $this->em->persist($url);
     $this->addTags2Page($values['tags'], $page);
     $this->em->persist($page);
     $this->em->flush();
     $this->em->commit();
     return $page;
 }
开发者ID:blitzik,项目名称:CMS,代码行数:36,代码来源:PagePersister.php

示例6: createRole

 /**
  * @param array $values
  * @return Role
  * @throws RoleAlreadyExistsException
  * @throws RoleMissingException
  */
 public function createRole(array $values)
 {
     $parentRole = null;
     if ($values['parent'] !== null) {
         $parentRole = $this->em->find(Role::class, $values['parent']);
         if ($parentRole === null) {
             throw new RoleMissingException();
         }
     }
     $role = new Role($values['name'], $parentRole);
     $role = $this->em->safePersist($role);
     if ($role === false) {
         throw new RoleAlreadyExistsException();
     }
     $this->onSuccessRoleCreation($role);
     return $role;
 }
开发者ID:blitzik,项目名称:CMS,代码行数:23,代码来源:RolePersister.php

示例7: registerUser

 /**
  * @param User $newUser
  * @param Invitation $invitation
  * @return User
  * @throws DuplicateUsernameException
  * @throws DuplicateEmailException
  * @throws InvalidUserInvitationEmailException
  */
 public function registerUser(User $newUser, Invitation $invitation)
 {
     if ($newUser->email !== $invitation->email) {
         throw new InvalidUserInvitationEmailException();
     }
     $this->em->beginTransaction();
     $user = $this->em->safePersist($newUser);
     if ($user === false) {
         $this->em->rollback();
         // e.g. when two users are trying to register
         // at the same time on the same Invitation
         if ($this->usersReader->isEmailRegistered($newUser->email)) {
             $this->invitationsWriter->removeInvitation($invitation);
             throw new DuplicateEmailException();
         }
         if ($this->usersReader->isUsernameRegistered($newUser->username)) {
             throw new DuplicateUsernameException();
         }
     }
     $this->invitationsWriter->removeInvitation($invitation);
     $this->em->commit();
     return $user;
 }
开发者ID:blitzik,项目名称:vycetky-doctrine,代码行数:31,代码来源:UserSystemCreator.php

示例8: create

 /**
  * @param array $values
  * @param User|null $user
  * @return ValidationObject
  */
 private function create(array $values, User $user = null)
 {
     if ($user === null) {
         $user = new User($values['username'], $values['email'], $values['password']);
     }
     $this->em->beginTransaction();
     $user->setUsername($values['username']);
     $user->setEmail($values['email']);
     $user->setPassword($values['password']);
     $user->setFirstName($values['first_name']);
     $user->setLastName($values['last_name']);
     $validationObject = new ValidationObject();
     $role = $this->getRole($values['role'], $validationObject);
     if (!$validationObject->isValid()) {
         $this->em->rollback();
         return $validationObject;
     }
     $user->addRole($role);
     $newUser = $this->em->safePersist($user);
     if ($newUser === false) {
         // username or email already exists
         if ($this->usernameExists($values['username'])) {
             $validationObject->addError('users.user.form.messages.usernameExists', FlashMessage::WARNING);
         }
         if ($this->emailExists($values['email'])) {
             $validationObject->addError('users.user.form.messages.emailExists', FlashMessage::WARNING);
         }
     }
     if ($validationObject->isValid()) {
         $this->onSuccessUserCreation($user);
         $this->em->commit();
     } else {
         $this->em->rollback();
     }
     return $validationObject;
 }
开发者ID:blitzik,项目名称:CMS,代码行数:41,代码来源:UserPersister.php


注:本文中的Kdyby\Doctrine\EntityManager::safePersist方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。