我想把我的重度治疗转移到。信使.
我创建了一个DownloadTask实体,我试图在过程中更新它,但它不工作。我的实体没有在数据库中更新。
你有什么想法吗?
我还在学习,所以我希望我的代码不会太伤你们的眼睛。
<?php
namespace App\MessageHandler;
use App\Message\GetSellerList;
use App\Repository\DownloadTaskRepository;
use App\Service\EbayDL;
use Doctrine\DBAL\ConnectionException;
use Doctrine\ORM\EntityManagerInterface;
use DTS\eBaySDK\Trading\Types\GetSellerListResponseType;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use GuzzleHttp\Promise;
class GetSellerListHandler implements MessageHandlerInterface
{
/**
* @var EbayDL
*/
private $ebayDL;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var \App\Entity\DownloadTask
*/
private $task;
/**
* @var DownloadTaskRepository
*/
private $downloadTaskRepository;
public function __construct(EbayDL $ebayDL, EntityManagerInterface $entityManager, DownloadTaskRepository $downloadTaskRepository, LoggerInterface $logger)
{
$this->ebayDL = $ebayDL;
$this->entityManager = $entityManager;
$this->logger = $logger;
$this->downloadTaskRepository = $downloadTaskRepository;
}
/**
* @param GetSellerList $getSellerList
* @throws ConnectionException
* @throws \Exception
* @throws \Throwable
*/
public function __invoke(GetSellerList $getSellerList)
{
$task = $this->downloadTaskRepository->find($getSellerList->getDownloadTaskId());
$this->clearDatabase();
$listingInfos = $this->ebayDL->getInformation();
$totalNumberOfPages = $listingInfos["totalNumberOfPages"];
$promises = (function () use ($totalNumberOfPages){
for ($page = 1; $page <= $totalNumberOfPages; $page++) {
yield $this->ebayDL->getProductsByPageAsync($page);
}
})();
$eachPromise = new Promise\EachPromise($promises, [
'concurrency' => 6,
'fulfilled' => function(GetSellerListResponseType $response) {
$products = $this->ebayDL->parseSellerListResponse($response);
foreach ($products as $product) {
try {
$this->entityManager->persist($product);
$this->entityManager->flush();
}catch (\Exception $e) {
$this->logger->error("Failed to store a product");
$this->logger->error($e->getMessage());
if (!$this->entityManager->isOpen()) {
// https://stackoverflow.com/questions/14258591/the-entitymanager-is-closed
$this->entityManager = $this->entityManager->create(
$this->entityManager->getConnection(),
$this->entityManager->getConfiguration()
);
}
}
}
}
]);
$eachPromise->promise()->wait();
$this->task
->setProgress(100)
->setCompletedAt(new \DateTime('NOW'));
$this->entityManager->flush();
}
/**
* @throws ConnectionException
* @throws \Doctrine\DBAL\DBALException
*/
private function clearDatabase()
{
$connection = $this->entityManager->getConnection();
$connection->beginTransaction();
try {
$connection->query('SET FOREIGN_KEY_CHECKS=0');
$connection->query('DELETE FROM product');
$connection->query('SET FOREIGN_KEY_CHECKS=1');
$connection->commit();
} catch (\Exception $exception) {
$connection->rollback();
}
}
}
应该
$task = $this->downloadTaskRepository->find($getSellerList->getDownloadTaskId());
做
$this->task = $this->downloadTaskRepository->find($getSellerList->getDownloadTaskId());
?