Search code examples
phpsymfonydoctrine-ormfosrestbundle

HTTP 500 error with FOSRestBundle when getting an entity with relations


I'm starting with FOSRestBundle and when I get the values of an entity without relations and display it in the browser, I have no problem. But when I try to get an entity with relations, it shows me an error with code: 500.

Here is the code:

app/config/config.yml:

fos_rest:
    routing_loader:
        default_format: json
    param_fetcher_listener: true
    body_listener: true
    format_listener: true
    view:
        view_response_listener: 'force'

ApiRestBundle/Controller/UserController (this works fine)

   /**       
    * @return array       
    * @Rest\Get("/users")       
    * @Rest\View()       
    */ 

public function getUsersAction()
{
    $response = array();

    $em = $this->getDoctrine()->getManager(); 
    $users = $em->getRepository('CASUsuariosBundle:User')->findAll();

    $view = $this->view($users);

    return $this->handleView($view);
}

APIRestBunde/Controller/CategoryController (this doesn't works)

/**       
* @return array       
* @Rest\Get("/categories")       
* @Rest\View()       
*/ 

public function getCategoriesAction()
{
    $response = array();

    $em = $this->getDoctrine()->getManager(); 
    $categories = $em->getRepository('CASEventBundle:Category')->findAll();

    $view = $this->view($categories);

    return $this->handleView($view);   
}

the error code:

{"error":{"code":500,"message":"Internal Server Error","exception":[{"message":"Notice: Undefined index: name","class":"Symfony\Component\Debug\Exception\ContextErrorException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"C:\xampp\htdocs\CASPruebas\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php","line":1758,"args":[]}...


Solution

  • your problem is a little bit complicated to solve.

    This error code can mean a lot of different things!

    But I think it is not directly a FOSRestBundle problem. Maybe you have a relation problem on your Category entity...

    What is the result of this: doctrine:schema:validate

    Edit : Maybe it will be more simple to solve it if you give us the full error code.