Pergunta

the error is: The identifier id is missing for a query of Cocktails\RecipesBundle\Entity\Ingredient

So, the problem is that controller doesn't get the parameter ingredientId, I've tried many examples and tutorials but nothing helped. maybe there's some easy obvious mistake which I am missing? How could I pass parameters from ajax function to controller?

Foi útil?

Solução

You're creating a new and empty Request object which obviously doesn't have the request information of the current request... it will be empty.

You can add a parameter to addIngredientToUser called $request and using type-hinting as Request $request. This way Symfony2 will give you the actual request object of the current request populated with the correct request information.

Outras dicas

Working code are

public function addIngredientToUserAction(Request $request) {
    $usr = $this->getUser();
    $id = $request->get('ingredient');
    $ingredientEntity = $this->getDoctrine()->getRepository('CocktailsRecipesBundle:Ingredient')->find($id);
    $usr->addIngredient($ingredientEntity);

    return $this->render('CocktailsRecipesBundle:Default:menu.html.twig');
}

In your comment for previous answer you have made a mistake

$id = $request->get('id');
                     ^^ - should be 'ingredient'
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top