/** * @param Category $category * @throws \Exception */ public function tryDelete(Category $category) { $this->tryValidate($category); $this->em->transactional(function () use($category) { $this->em->remove($category); }); }
/** * @param Article $article * @throws \Exception */ public function tryDelete(Article $article) { $this->tryValidate($article); $this->em->transactional(function () use($article) { // TODO 画像の削除処理必要 $this->em->remove($article); }); }
/** @inheritDoc */ public function execute(array $fixtures, $append = false) { $executor = $this; $this->em->transactional(function (EntityManagerInterface $em) use($executor, $fixtures, $append) { if ($append === false) { $executor->purge(); } foreach ($fixtures as $fixture) { $executor->load($em, $fixture); } }); }
/** * Uses the provided callback to import entities. * * @param callable $callback */ protected function importFromCallback($callback) { $import = function (ObjectManager $objectManager) use($callback) { $decorator = new DetachingObjectManagerDecorator($objectManager); call_user_func($callback, $decorator); // Flush manually to ensure that persisted entities are detached. $decorator->flush(); }; $this->entityManager->transactional($import); }
/** * インポートを試み、エラーの場合は適切なExceptionをthrowする * @param DnsUserImportDto $dto * @throws InvalidArgumentException * @throws FormValidationException * @todo メソッドが肥大化してきたので分割したい */ public function tryImportAndGetResultCount(DnsUserImportDto $dto) { if (is_null($dto)) { throw \InvalidArgumentException('DtoオブジェクトがNullです。'); } $filePath = $dto->getImportFile()->getRealPath(); if (!file_exists($filePath)) { throw \InvalidArgumentException('インポートファイルが見つかりません。'); } // 登録されているusernameデータを取得 $userNames = $this->em->getRepository(DnsUser::class)->findUserNameAll(); $file = new \SplFileObject($filePath); $file->setFlags(\SplFileObject::READ_CSV); $updateCount = 0; $this->em->transactional(function () use($file, $dto, $userNames, &$updateCount) { $idx = 0; $updateCount = 0; foreach ($file as $row) { $idx++; // ヘッダ行を無視するオプションを選択かつヘッダ行の場合スキップ if ($dto->getIsIgnoreHeaderLine() === DnsUserImportDto::IS_IGNORE_HEADER_YES && $idx === 1) { continue; } // 列数が想定通りでない場合スキップ if (count($row) !== DnsUserImportDto::FILE_COLUMN_COUNT) { continue; } list(, $userName, $password, $controlNo, $comment, $encryptType) = $row; mb_convert_variables("UTF-8", "SJIS", $userName, $password, $controlNo, $comment, $encryptType); if (in_array($userName, $userNames)) { // 既存のデータは上書きしないオプション選択の場合スキップ if ($dto->getIsUpdated() === DnsUserImportDto::IS_UPDATED_NO) { continue; } $entity = $this->em->getRepository(DnsUser::class)->findOneByUserName($userName); $passwordBackup = $entity->getPassword(); $entity->setPassword($password)->recoveryPasswordIfNull($passwordBackup); } else { $entity = (new DnsUser())->setUserName($userName)->setPassword($password); } $entity->setControlNo($controlNo)->setComment($comment)->setEncryptType(intval($encryptType)); $this->tryValidate($entity); $this->em->persist($entity); $updateCount++; } }); return $updateCount; }
/** * {@inheritdoc} */ public function transactional($func) { return $this->wrapped->transactional($func); }