В чем проблема с моей функцией хранилища? - PullRequest
0 голосов
/ 15 октября 2019

Я получаю следующую ошибку при попытке использовать это в моем контроллере

Возвращаемое значение 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;
    }
}

Ответы [ 2 ]

0 голосов
/ 16 октября 2019

Я думаю, вам просто нужно изменить тип возврата

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;
    }
}
0 голосов
/ 15 октября 2019

только что изменил код моего AccountRepository и добавил массив в качестве типа возврата

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

...