-2

当我尝试在控制器中使用它时出现以下错误

App\Repository\AccountRepository::findOneByAccountCode() 的返回值必须是 App\Repository\Bank 的实例或 null,返回 App\Entity\Account 的实例

/**
 * @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;
    }
}
4

2 回答 2

0

只是将代码更改为我的 AccountRepository 并添加数组作为返回类型

function findOneByAccountCode(string $accountCode): ?array{
        return $this->createQueryBuilder('a')
            ->innerJoin('a.bank', 'b')
            ->where('a.code = :code')
            ->setParameter('code', $accountCode)
            ->getQuery()
            ->getResults();
    } 

于 2019-10-15T20:28:51.423 回答
0

我想你只需要改变返回类型

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;
    }
}
于 2019-10-16T07:43:53.107 回答