How to Save Form Data to DB with Cakephp

Asked

Viewed 137 times

0

Good Morning

I need help because I am creating a form to save only dates......

the view is already created with 4 fields for dates... and there in the controller I’m half lost.

I’ve debugged the $data and it brings the information in the bank inserted in the Marra...so far so good. the commented part I left for when bring the saved dates and inform whether it worked or not...

tela campos com datas

Controller

public function desligamentocliente()
{
    //$this->verifica_ausencia_dados($this->request->data['idCliente']);
    $this->loadModel( "DesligamentoCliente" );               

    $idCliente = ( isset( $this->request->params['pass'][0] ) ) ? $this->request->params['pass'][0] : 0;

    $dados = $this->DesligamentoCliente->find( "all", array( "conditions" => array("_esc_codigo" => $idCliente )));

    /*
    $dados == 1 ? $this->Desligamentos->DesligamentoCliente->save("data_financeiro", "data_fiscal", "data_contabil", "data_pessoal") : 

        $this->request->data['DesligamentosCliente']['data_financeiro'] = General::formata_data_mysql($this->request->data['DesligamentosCliente']['data_financeiro'] );
        $this->request->data['DesligamentosCliente']['data_fiscal']     = General::formata_data_mysql($this->request->data['DesligamentosCliente']['data_fiscal'] );
        $this->request->data['DesligamentosCliente']['data_contabil']   = General::formata_data_mysql($this->request->data['DesligamentosCliente']['data_contabil'] );
        $this->request->data['DesligamentosCliente']['data_pessoal']    = General::formata_data_mysql($this->request->data['DesligamentosCliente']['data_pessoal'] );


        if ($this->Desligamentos->desligamentocliente->updateAll( array("data_financeiro" => $data_financeiro, "data_fiscal" => $data_fiscal, "data_contabil" => $data_contabil, "data_pessoal" => $data_pessoal)
                                                                )
            ) {
            $msg = "Ocorreu um erro ao tentar salvar as datas";

            if ($this->Desligamentos->desligamentocliente->save($this->request->data)) {
                $msg = "Datas salvas com sucesso";
                $class = "alert alert-success";
            }
        }

$this->Session->setFlash($msg, "alerta", array("class" => $class));
$this ->redirect("/clientes");
*/ }

Thanks for your help... =)

  • I didn’t get it right, he is saving in the bank, but with the wrong dates? in what format is your dates in the view (format br? d/m/Y?)

  • I will try to explain better @Jefersonassis , what I’m having difficulty is to mondar if...(ñ know if that’s right) that receive the dates informed in the form and save them in the bank. I debugged the variable data...pretty much the way it is..."print_r($data); die()" and it showed me data that I manually entered in table q it should receive.

  • I’ll post the answer, maybe that’s what you need

  • So what I need......

  • Thanks @Jefersonassis... very grateful =)

  • I answered, see if it helps you answer.

Show 1 more comment

1 answer

1


You can check if a post is coming and redeem the data sent by the form

If you are using the Cakephp 3.0, add in the first line of your controller after the <?php

use Cake I18n Time;

if($this->request->is('post')):
    $data = $this->request->data;

    foreach($data['DesligamentosCliente'] as &$value):
        $value = Time::createFromFormat('d/m/Y', $value)->format('Y-m-d');
    endforeach;

    pr($data);
endif;

Browser other questions tagged

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