Change the status of a record in the database by ID via post method

Asked

Viewed 59 times

0

I need to change the status of an object registered in the database, from the ID of this object, once I click the corresponding button. How do I move on to my role in controller the ID of that object?

What I should insert into my HTML button?

HTML button:

<button type="submit" class="btn btn-outline-danger" name="acao" value="desligar" id="{{ cooperado.getId() }}">Desligar</button>

Function in the controller:

public function acaoBotoes()
{
    switch ($_POST['acao']) {
        case 'desligar':
            $id = $_POST['???'];
            $em = $this->getDoctrine()->getManager();
            $cooperado = $em->getRepository(Cooperado::class)->find($id);

            $cooperado->setStatus('DE')
                ->setDataDesligamento(\DateTime::createFromFormat('Y-m-d', date('Y-m-d')));
            $em->flush();

            return $this->redirectToRoute('cooperados');

            break;

In the $id variable I need to get the ID from the view button, then handle the status change function of this object. This case will handle the status change. I need to click "off" and change the status of an Active to Off cooperator, as below: inserir a descrição da imagem aqui

1 answer

0


You can add a:

<input type="hidden" value="id" name="id">

And so I recover:

$_POST['id']

  • Excellent, Marcelo. That worked perfectly. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.