How to save data from a form in DB with CakePHP

0

Good Morning

I need help because I'm creating a form to save only dates ... and I'm having trouble creating this code ... for lack of experience ...

the view is already created with 4 fields for dates ... and then in the controller I'm kind of lost.

I have already debugged $ data and it after the information in the bank inserted in the marra ... so far so good. the commented part I left for when troxer the dates saved and inform if it worked or not ...

Controller

publicfunctiondesligamentocliente(){//$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 the help ... =)

    
asked by anonymous 18.11.2015 / 13:20

1 answer

1

You can check if a post is coming and retrieve the data submitted by the form

If you're using CakePHP 3.0 , add in the first line of your controller after <?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;
    
18.11.2015 / 14:20