0

I am new to symfony and I am trying to pass a object from a list to a new page using:

<a href="{{ path('fooUpdate', { 'id': item.id }) }}">Update</a>

The list ({% for item in list %}) of Foo objects is fine, the objects are fully loaded from the database, but the link does not work because of this Doctrine error:

The identifier id is missing for a query of AppBundle\Entity\Foo

Here is the Foo class:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/** 
 * @ORM\Entity
 * @ORM\Table(name="foo") 
 */
class Foo {

/** 
 * @ORM\Id 
 * @ORM\Column(type="integer") 
 * @ORM\GeneratedValue
 */
private $id;

/** 
 * @ORM\Column(type="string")    
 */
private $description;

//getters and setters

And here is the controller method:

/**
 * @Route("/foo/fooUpdate/", name="fooUpdate")
 */
public function updateAction($id, Request $request) {
    $foo = $this->getDoctrine()->getRepository('AppBundle:Foo')->find($id);
    return $this->render('/foo/fooUpdate.html.twig', array(
            'foo' => $foo
    ));
}

The link itself looks like it is working, because the URL of the error is:

http://127.0.0.1:8000/foo/fooUpdate/?id=1

So what am I missing?

Also, is there any way of doing this using POST, so the id will not appear in the URL?

2 Answers 2

2

You are missing the id in the route:

@Route("/foo/fooUpdate/{id}", name="fooUpdate")
Sign up to request clarification or add additional context in comments.

2 Comments

Oh thanks! It worked, but any idea on how to do it via post?
Assuming you have a form or similar to send your post data. You have to configure your route as * @Method({"POST"}) then in your controller action get you post vars. Saludos
0

Actually, you should also set defaults:

/**
 * @Route("/foo/fooUpdate/{id}",
 *      defaults={"id" = 0},
 *      name="fooUpdate")
 */

The defaults are used in case you don't pass in a parameter.

As far as not setting the id in the URL, look into sessions: https://symfony.com/doc/current/components/http_foundation/sessions.html

You should be able to do that, but it's more work (coding).

Comments

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.