Home » Php » php – push data into an array of objects Symfony

php – push data into an array of objects Symfony

Posted by: admin February 25, 2020 Leave a comment

Questions:

hi guys i need a little help here i have an array here and that array contains objects now each of these objects contains a lead with its status and its total count for the day now i am also getting the date on which day or month were these created not i am not able to figure out how to pass the date into that array for that specific data

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);

and currently in response i get this

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

now each of those objects are created on a date i have the date in the $startDate variable but i am not able to figure out how to pass their startDate inside each object sorry if i used any wrong term. And i would like to change the name of the key from 1 to count. i will really thankful if some can help me here.

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
            }
        ],
        [],
        []
    ]
How to&Answers: