當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。