Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony使用post请求更新数据_Php_Symfony_Post_Symfony4_Symfony Forms - Fatal编程技术网

Php Symfony使用post请求更新数据

Php Symfony使用post请求更新数据,php,symfony,post,symfony4,symfony-forms,Php,Symfony,Post,Symfony4,Symfony Forms,我当前正在尝试更新表中的一个人。该表如下所示: /** * @Route("/user/persons/{id}", name="updatePerson") */ public function updatePerson(int $id): Response { $entityManager = $this->getDoctrine()->getManager(); $per

我当前正在尝试更新表中的一个人。该表如下所示:

/**
     * @Route("/user/persons/{id}", name="updatePerson")
     */
    public function updatePerson(int $id): Response
    {
        $entityManager = $this->getDoctrine()->getManager();
        $person = $entityManager->getRepository(Person::class)->find($id);

        if (!$person) {
            throw $this->createNotFoundException(
                'No person found for id '.$id
            );
        }

        $person->setFirstName('FirstName');
        $person->setLastName('Lastname');
        $person->setNin('00.09.69-420.2');
        $entityManager->flush();

        $persons = $this->getDoctrine()->getRepository(Person::class)->findAll();
        return $this->render('user/person/index.html.twig', [
            'persons' => $persons,
        ]);
    }

当我点击提交按钮时,我希望能够更新名字和姓氏以及nin。 我的细枝文件如下所示:

{% block body %}
    <h1>All our Patients</h1>
    <table class="table">
        <thead class="thead-dark">
        <tr>
            <th scope="col">#</th>
            <th scope="col">Firstname</th>
            <th scope="col">Lastname</th>
            <th scope="col">NIN</th>
            <th scope="col">Update</th>
        </tr>
        </thead>
        <tbody>
        {% for person in persons %}
        <tr>
            <form action="{{ path('updatePerson', {"id": person.id}) }}" method="post">
                <td>{{ person.id }}</td>
                <td><input type="text" id="firstName"  value="{{ person.firstName }}"></td>
                <td><input type="text" id="lastName"  value="{{ person.lastName }}"></td>
                <td><input type="text" id="nin" value="{{ person.nin }}"></td>
                <td><input class="btn btn-primary" type="submit" value="Submit"></td>
            </form>
        </tr>
        {% endfor %}
        </tbody>
    </table>
{% endblock %}
当前,当我单击更新时,我只需填写“setFirstName('FirstName');”中的内容。我真的不知道如何传递正确的数据,使其正确更新?如何通过按submit获得要进行的更改?

快速修复(不推荐)

您应该为文本输入标记添加
name
属性

  <td><input type="text" id="firstName" name="firstName" value="{{ person.firstName }}"></td>
  <td><input type="text" id="lastName" name="lastName" value="{{ person.lastName }}"></td>
  <td><input type="text" id="nin" name="nin" value="{{ person.nin }}"></td>

但是对于最佳实践,您应该使用并添加一个提示:
$person->setFirstName($request->get('FirstName')
/**
 * @Route("/user/persons/{id}", name="updatePerson")
 */
public function updatePerson(Request $request, Person $person): Response
{
    $entityManager = $this->getDoctrine()->getManager();

    $person->setFirstName($request->request('firstName'));
    $person->setLastName($request->request('lastName'));
    $person->setNin($request->request('nin');   

    $entityManager->flush();

    $persons = $this->getDoctrine()->getRepository(Person::class)->findAll();

    return $this->render('user/person/index.html.twig', [
        'persons' => $persons,
    ]);
}