首页 > 解决方案 > 如何修复我的“注意:未定义的偏移量:4”错误

问题描述

我的代码有问题,当用户拥有所有“站”代码工作但如果他没有一个我有(未定义的偏移量*)错误,

我的代码看起来像那样(在我的控制器中)

public function index()
{
    $entityManager = $this->getDoctrine()->getManager();
    $stations = $this->getListOfStations();
    $stationsJson = [];

    $events = $entityManager->getRepository('App:User')
        ->getEventInRealTime($this->getUser());

    foreach ($stations as $station) {
        $stationsJson[] = [
            'typeEvents' => $events[$station->getId()],
        ];
    }

    return new JsonResponse(array('stations' => $stationsJson));
}


private function getListOfStations()
{
    /** @var UserInterface $user */
    $user = $this->getUser();
    $entityManager = $this->getDoctrine()->getManager();

    if (!$this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
        $stations = $user->getStations();
    } else {
        $stations = $entityManager->getRepository('App:Station')->findAll();
    }

    return $stations;
}

和我的存储库

public function getEventInRealTime($user)
{
    $qb = $this->createQueryBuilder('u');
    $events = $qb
        ->select('s.id as stationId, COUNT(e) as number, e.label as type')
        ->innerJoin('u.stations', 's')
        ->innerJoin('s.events', 'e')
        ->where('u = :user')
        ->setParameter('user', $user)
        ->andWhere('DAY(e.currentTime) =:day')
        ->setParameter('day', date('d'))
        ->andWhere('MONTH(e.currentTime) =:month')
        ->setParameter('month', date('m'))
        ->andWhere('YEAR(e.currentTime) =:year')
        ->setParameter('year', date('Y'))
        ->groupBy('s.id, type');

    $data = $events->getQuery()->getArrayResult();

    $result = [];

    foreach ($data as $row) {
        $result[$row['stationId']][] =
            [
                'number' => $row['number'],
                'type' => $row['type']
            ];
    }

    return $result;
}

我认为这是一个问题,当我执行 findAll() 时,他没有找到“站”,但我不知道如何解决它,当用户是超级管理员时,他可以看到所有站,但如果不是超级管理员他只能看到他的电台

标签: phpsymfonydoctrine

解决方案


推荐阅读