我在我的学说资料库中使用
return $qb->getQuery()->getSingleScalarResult();
getSingleScalarResult
的定义如下:
/**
* Gets the single scalar result of the query.
*
* Alias for getSingleResult(HYDRATE_SINGLE_SCALAR).
*
* @return mixed
*
* @throws NonUniqueResultException If the query result is not unique.
* @throws NoResultException If the query returned no result.
*/
public function getSingleScalarResult()
{
return $this->getSingleResult(self::HYDRATE_SINGLE_SCALAR);
}
因此,在我的服务中,我捕获了这些异常并尝试适当地处理它们:
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
public function getCustomerSpecificPrice(User $user, Product $product)
{
$initialContract = $this->em->getRepository(Contract::class)
->getInitialContract($user);
try {
$condition = $this->em->getRepository(ContractCondition::class)
->getValidForProduct($initialContract, $product);
return $condition->getPrice();
} catch (NoResultException $e) {
return $product->getPrice();
} catch (NonUniqueResultException $e) {
return $product->getPrice();
}
}
基本上,我尝试查找有效的ContractCondition,如果没有(或太多),我将使用标准产品价格。
未捕获异常。为什么?文档在side note上说:
与普通异常一样,这些Error异常会冒泡直到 他们到达第一个匹配的捕获块。如果没有匹配项 块,然后安装任何默认的异常处理程序 set_exception_handler()将被调用,如果没有默认值 异常处理程序,那么该异常将被转换为致命错误 错误,并且将像传统错误一样处理。
在项目中加载一些灯具时,我遇到了异常:
$ bin/console doctrine:fixtures:load --env=test -n -v
> purging database
> loading [1] AppBundle\DataFixtures\ORM\LoadPageData
> loading [2] AppBundle\DataFixtures\ORM\LoadAdminData
> loading [3] AppBundle\DataFixtures\ORM\LoadCompanyData
> loading [4] AppBundle\DataFixtures\ORM\LoadSchoolData
> loading [5] AppBundle\DataFixtures\ORM\LoadProductData
> loading [6] AppBundle\DataFixtures\ORM\LoadBundleData
> loading [7] AppBundle\DataFixtures\ORM\LoadAvailableExamLanguagesData
> loading [8] AppBundle\DataFixtures\ORM\LoadInstructorData
> loading [9] AppBundle\DataFixtures\ORM\LoadStudentData
> loading [10] AppBundle\DataFixtures\ORM\LoadTimetableData
> loading [11] AppBundle\DataFixtures\ORM\LoadInstructorScheduleData
> loading [12] AppBundle\DataFixtures\ORM\LoadContractData
[Doctrine\ORM\NoResultException]
No result was found for query although at least one row was expected.
Exception trace:
() at /var/www/project/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:807
Doctrine\ORM\AbstractQuery->getSingleResult() at /var/www/project/src/AppBundle/Repository/ContractRepository.php:475
AppBundle\Repository\ContractRepository->getInitialContract() at /var/www/project/src/AppBundle/Service/PricingService.php:25
AppBundle\Service\PricingService->getCustomerSpecificPrice() at /var/www/project/src/AppBundle/Service/ContractService.php:231
AppBundle\Service\ContractService->createContract() at /var/www/project/src/AppBundle/DataFixtures/ORM/LoadContractData.php:79
AppBundle\DataFixtures\ORM\LoadContractData->load() at /var/www/project/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/AbstractExecutor.php:121
Doctrine\Common\DataFixtures\Executor\AbstractExecutor->load() at /var/www/project/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php:83
Doctrine\Common\DataFixtures\Executor\ORMExecutor->Doctrine\Common\DataFixtures\Executor\{closure}() at n/a:n/a
call_user_func() at /var/www/project/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:233
Doctrine\ORM\EntityManager->transactional() at /var/www/project/app/cache/test/appTestDebugProjectContainer.php:10714
DoctrineORMEntityManager_0000000025b0f31f000000000a6fef789ffd066567cf09dff04afe3a5f2f5e93->transactional() at /var/www/project/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php:85
Doctrine\Common\DataFixtures\Executor\ORMExecutor->execute() at /var/www/project/vendor/doctrine/doctrine-fixtures-bundle/Command/LoadDataFixturesDoctrineCommand.php:118
Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand->execute() at /var/www/project/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:264
Symfony\Component\Console\Command\Command->run() at /var/www/project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:854
Symfony\Component\Console\Application->doRunCommand() at /var/www/project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:201
Symfony\Component\Console\Application->doRun() at /var/www/project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:81
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:125
Symfony\Component\Console\Application->run() at /var/www/project/bin/console:29