1

Be gentle, I'm a hacker not a professional developer!

I think I'm close, but I am getting tripped up by the different ways that a Controller can retrieve data and return it as JSON (to a data table). I thought it would be simple enough to clone another feature that was working, but oh no!

This is an old (version 3.4) PHP Symfony. [In parallel I've been prep'ing a test site to start the upgrade work].

Current State of Play: Just a single row of many is returned. But it is in the right JSON format (and happily rendered by the data table).

The controller function is ...

/**
     * @Route("/{slug}/feed", name="project_feed")
     * @Method({"GET", "POST"})* 
     * @param Request $request
     * @param Project $project
     * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function getProjectFeed(Request $request, Project $project)
    {
        $em = $this->getDoctrine()->getManager(); //Get feed for current project
        $query = $em->createQuery('SELECT a FROM AppBundle:ProjectFeed a WHERE a.project = :project_id');
        $this->logger->debug("ProjectController.getProjectFeed ... query == ", [$query]);
        $query->setParameter('project_id', $project->getId());
        $feed = $query->getResult(); // Retrieve feed if none exist return empty JSON response
        
        // Doesn't populate Modifier
        //$feed = $this->getDoctrine()->getRepository("AppBundle:Project")->getProjectFeed($project);
        
        $serializer = $this->get('app.service.serializer');
        return new Response($serializer->serializeEntity($feed, array('data_table')));
    }

Previously: I have had all rows returned, but there is a relationship in the entity that doesn't get populated and is completely dropped from the JSON.

The repository function was ...

     * @param Project $project
     * @return array
     */
    public function getProjectFeed(Project $project)
    {
        $em = $this->getEntityManager();
        $sql = 'SELECT a FROM AppBundle:ProjectFeed a WHERE a.project = :project_id';
        $query = $em->createQuery($sql);
        $query->setParameter('project_id', $project->getId());
        return $query->getResult(Query::HYDRATE_ARRAY);
    }

Any pointers on the correct way to do this so I ended up with all rows returned and the correctly formatted JSON with the related entity data populated would be most welcome. Thanks.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.