php - 如何修复我的“注意:未定义的偏移量: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() 时,他没有找到“站”,但我不知道如何解决它,当用户是超级管理员时,他可以看到所有站,但如果不是超级管理员他只能看到他的电台
解决方案
推荐阅读
- rocksdb - 是否可以不覆盖 RocksDB 中的早期值?
- python - 使用 OpenVINO 运行 Facenet
- xamarin.forms - 更改列表视图中的所有 xamarin 表单按钮背景
- javascript - SocketIO:如何从前端克服 CORS?
- api - 谷歌地图 api - 添加 alt 属性以关闭标记按钮
- java - 对于每个键超过 10000 个元素,数据流 CoGroupByKey 非常慢
- wix - 将命令行参数传递给 WiX 自定义操作
- python - 通过 Python 中的类方法更新 __init__ 元素
- bash - 使用特殊字符删除 sed 中的特定字符范围
- django - DJango:单个项目中应用程序的多个实例