将数据推入对象数组Symfony

时间:2020-02-13 13:52:21

标签: php symfony

嗨,我需要一些帮助,我在这里有一个数组,该数组包含对象,现在这些对象中的每一个都包含潜在客户及其状态和当日总计数,现在我也可以获取到当天的日期或这些是不是创建的月份,我无法弄清楚如何将日期传递给该数组以获取特定数据

public function leadsByMonth(ServerRequestInterface $request): HttpResponseInterface
    {
        $params = $request->getQueryParams() ?? [];
        $result = null;
        $dates = $this->getDates($params);
        $datesFromRange = $this->getDatesFromRange($params['formatType'], $dates);
        $queryBuilder = $this->entityManager->getRepository(Lead::class)->createQueryBuilder('lead');
        $expr = $queryBuilder->expr();
        $selectQuery = '';
        $rootAlias = $queryBuilder->getRootAliases()[0];
        if (isset($params['filterType'])) {
                $selectQuery .= $rootAlias.'.'.$params['filterType'];
                $selectQuery .= ',';
        }
        $groupByQuery = $selectQuery;
        $selectQuery .= 'count(lead.id)';
        $format = null;
        foreach ($datesFromRange as $date) {
            $startDate = $date['startDate'];
            $endDate = $date['endDate'];
            $queryBuilder->select(sprintf('%s', $selectQuery))
                ->where($expr->andX(
                    $expr->gte('lead.dateCreated', $expr->literal($startDate->format('c'))),
                    $expr->lt('lead.dateCreated', $expr->literal($endDate->format('c')))
                ));
                $queryBuilder->groupBy(sprintf('lead.%s', $params['filterType']));

              $result[] = $leads = $queryBuilder->getQuery()->getScalarResult();

//              dump($result);

            //            $result[] = ['count' => $leads[0]['1'], $params['formatType'] => 'month' === $params['formatType'] ? $startDate->format('F')
//                : $startDate->format('Y-m-d'), ];
//            dump($leads);
        }
        return new JsonResponse($result);

目前,我得到了这个

[
    [
        {
            "status": "NEW",
            "1": 597
        },
        {
            "status": "CONVERTED",
            "1": 37
        }
    ],
    [
        {
            "status": "NEW",
            "1": 365
        },
        {
            "status": "CONVERTED",
            "1": 255
        }
    ],
    [
        {
            "status": "CONVERTED",
            "1": 22
        },
        {
            "status": "NEW",
            "1": 54
        }
    ],
    [],
    []
]

现在每个对象都是在某个日期创建的,我在$ startDate变量中有该日期,但是如果我使用了任何错误的术语,我将无法弄清楚如何在每个对象内部传递它们的startDate。我想将键的名称从1更改为count。如果有人能在这里帮助我,我将非常感谢。

i am expecting like this

 [
        [
            {
                "status": "NEW",
                "1": 597,
                "format": 2019-12-29 
            },
            {
                "status": "CONVERTED",
                "1": 37,
                "format: 2019-12-30
            }
        ],
        [
            {
                "status": "NEW",
                "1": 365,
                "format":2019-12-31
            },
            {
                "status": "CONVERTED",
                "1": 255,
                "format": 2020-01-01
            }
        ],
        [
            {
                "status": "CONVERTED",
                "1": 22,
                "format": 2020-01-02
            },
            {
                "status": "NEW",
                "1": 54,
                "format": 2020-01-03
            }
        ],
        [],
        []
    ]

0 个答案:

没有答案