我的存储库功能有什么问题?
What's the issue on my repository function?
当我尝试在我的控制器中使用它时出现以下错误
Return value of
App\Repository\AccountRepository::findOneByAccountCode() must be an
instance of App\Repository\Bank or null, instance of
App\Entity\Account returned
/**
* @param Request $request
* @param string $accountCode
* @return Response
* @throws EntityNotFoundException
*/
public function somefunction(Request $request, string $accountCode)
{
/** @var BankRepository $acRepository */
$acRepository = $this->getDoctrine()->getRepository(Account::class);
$bank = $acRepository->findOneByAccountCode($accountCode);
}
存储库代码
public function findOneByAccountCode(string $accountCode): ?Bank
{
try {
return $this->createQueryBuilder('a')
->innerJoin('a.bank', 'b')
->where('a.code = :code')
->setParameter('code', $accountCode)
->getQuery()
->getOneOrNullResult();
}catch (NonUniqueResultException $e) {
return null;
}
}
刚刚将代码更改为我的 AccountRepository 并将数组添加为 return 类型
function findOneByAccountCode(string $accountCode): ?array{
return $this->createQueryBuilder('a')
->innerJoin('a.bank', 'b')
->where('a.code = :code')
->setParameter('code', $accountCode)
->getQuery()
->getResults();
}
我想你只需更改 return 类型的
public function findOneByAccountCode(string $accountCode): ?Bank
{
try {
return $this->createQueryBuilder('a')
->innerJoin('a.bank', 'b')
->where('a.code = :code')
->setParameter('code', $accountCode)
->getQuery()
->getOneOrNullResult();
} catch (NonUniqueResultException $e) {
return null;
}
}
来自
: ?Bank
至
: ?Account
public function findOneByAccountCode(string $accountCode): ?Account
{
try {
return $this->createQueryBuilder('a')
->innerJoin('a.bank', 'b')
->where('a.code = :code')
->setParameter('code', $accountCode)
->getQuery()
->getOneOrNullResult();
} catch (NonUniqueResultException $e) {
return null;
}
}
当我尝试在我的控制器中使用它时出现以下错误
Return value of App\Repository\AccountRepository::findOneByAccountCode() must be an instance of App\Repository\Bank or null, instance of App\Entity\Account returned
/**
* @param Request $request
* @param string $accountCode
* @return Response
* @throws EntityNotFoundException
*/
public function somefunction(Request $request, string $accountCode)
{
/** @var BankRepository $acRepository */
$acRepository = $this->getDoctrine()->getRepository(Account::class);
$bank = $acRepository->findOneByAccountCode($accountCode);
}
存储库代码
public function findOneByAccountCode(string $accountCode): ?Bank
{
try {
return $this->createQueryBuilder('a')
->innerJoin('a.bank', 'b')
->where('a.code = :code')
->setParameter('code', $accountCode)
->getQuery()
->getOneOrNullResult();
}catch (NonUniqueResultException $e) {
return null;
}
}
刚刚将代码更改为我的 AccountRepository 并将数组添加为 return 类型
function findOneByAccountCode(string $accountCode): ?array{
return $this->createQueryBuilder('a')
->innerJoin('a.bank', 'b')
->where('a.code = :code')
->setParameter('code', $accountCode)
->getQuery()
->getResults();
}
我想你只需更改 return 类型的
public function findOneByAccountCode(string $accountCode): ?Bank
{
try {
return $this->createQueryBuilder('a')
->innerJoin('a.bank', 'b')
->where('a.code = :code')
->setParameter('code', $accountCode)
->getQuery()
->getOneOrNullResult();
} catch (NonUniqueResultException $e) {
return null;
}
}
来自
: ?Bank
至
: ?Account
public function findOneByAccountCode(string $accountCode): ?Account
{
try {
return $this->createQueryBuilder('a')
->innerJoin('a.bank', 'b')
->where('a.code = :code')
->setParameter('code', $accountCode)
->getQuery()
->getOneOrNullResult();
} catch (NonUniqueResultException $e) {
return null;
}
}