如何修复我的“通知:未定义的偏移量:4”错误

时间:2019-11-05 00:46:58

标签: php symfony doctrine

我的代码有问题,当用户拥有所有“工作站”时,代码可以工作,但是如果他没有,则我会遇到((未定义偏移*)错误,

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

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()时他没有找到“工作站”,但是我不知道如何解决它,当用户是super_admin时,他可以看到所有工作站,但如果不是超级管理员,他只能看到自己的电台

0 个答案:

没有答案