문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top