0

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?

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

and what should i send in ajax as a parameter to controllers parameter $request?
You don't need to send anything, Request $request, Post $post and a few other parameters of action functions don't have to be explicitly passed, they are automatically filled in by Symfony2.
Try to debug -- check if the post data reaches your controller by using var_dump($_GET, $_POST, $_REQUEST);
all three arrays are empty. why doesn't it reach controller? maybe it's because it's going through routing and parameters dont get passed?
when changing POST to GET it shows that parameters are passed (var dump array(1) { ["{"ingredient":"2"}"]=> string(0) "" } but i'm still not able to reach it
|
0

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'

2 Comments

sorry about that, one comment(now deleted) suggested changing var names and so i did. this is not the case, it still doesnt work even with ingredient
Could you please do alert(ingredient) in JS before ajax() call?

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.