Error in generation of sql with datetime

Asked

Viewed 20 times

-1

I got the following method:

public function inserir (ClientesModelos $_cliente) : bool {

    $sql = 'INSERT INTO clientes (data, nome, sobreNome, nascimento, documento, telefone, celular, email, senha, bloqueio) VALUES (?,?,?,?,?,?,?,?,?,?)';

    $inserir = $this->pdo->prepare ($sql);

    $inserir->bindValue (1, $_cliente->getData());
    $inserir->bindValue (2, $_cliente->getNome());
    $inserir->bindValue (3, $_cliente->getSobreNome());
    $inserir->bindValue (4, $_cliente->getNascimento());
    $inserir->bindValue (5, $_cliente->getDocumento());
    $inserir->bindValue (6, $_cliente->getTelefone());
    $inserir->bindValue (7, $_cliente->getCelular());
    $inserir->bindValue (8, $_cliente->getEmail());
    $inserir->bindValue (9, $_cliente->getSenha());
    $inserir->bindValue (10, $_cliente->getBloqueio());

    return $inserir->execute();

}

Who asks for a object of class Customers and that is being generated as below:

$cliente = new ClientesModelos (
         'Carlos',
         'Alberto',
         new DateTime('1970/12/20', new DateTimeZone('America/Sao_Paulo') ),
         11111111111,
         1111111111,
         11111111111,
         '[email protected]',
         'aaaa',
         'nao' 
);

See the print_r:

classes\mvc\modelos\ClientesModelos Object
(
    [idClientes:classes\mvc\modelos\ClientesModelos:private] => 
    [data:classes\mvc\modelos\ClientesModelos:private] => DateTime Object
        (
            [date] => 2019-05-09 16:29:25.587117
            [timezone_type] => 3
            [timezone] => America/Sao_Paulo
        )

    [nome:classes\mvc\modelos\ClientesModelos:private] => Carlos
    [sobreNome:classes\mvc\modelos\ClientesModelos:private] => Alberto
    [nascimento:classes\mvc\modelos\ClientesModelos:private] => DateTime Object
        (
            [date] => 1970-12-20 00:00:00.000000
            [timezone_type] => 3
            [timezone] => America/Sao_Paulo
        )

    [documento:classes\mvc\modelos\ClientesModelos:private] => 11111111111
    [telefone:classes\mvc\modelos\ClientesModelos:private] => 1111111111
    [celular:classes\mvc\modelos\ClientesModelos:private] => 11111111111
    [email:classes\mvc\modelos\ClientesModelos:private] => [email protected]
    [senha:classes\mvc\modelos\ClientesModelos:private] => aaaa
    [bloqueio:classes\mvc\modelos\ClientesModelos:private] => 1
)

But when that object gets into the function insert is giving error in line 21 that is:

$inserir->bindValue (1, $_cliente->getData());

And the error that’s the one:

Recoverable fatal error: Object of class DateTime could not be converted to string in D:\Trabalhos\host\htdocs\mvc_crud_pdo\classes\mvc\modelos\ClientesDao.php on line 21

What to do?

1 answer

2


Missing the conversion of the Object of Datetime for String, adds ->format('Y-m-d') in the getData()

$inserir->bindValue (1, $_cliente->getData()->format('Y-m-d'));

and in Nascimento as well

$inserir->bindValue (4, $_cliente->getNascimento()->format('Y-m-d'));

if you want to add the time in the first bind getData()->format('Y-m-d H:i:s')

  • Yes, but I would like to pass as attribute of the new object Datetime ('1970/12/20', new Datetimezone('America/Sao_paulo') ), there in the object. How do I do it? That’s so I don’t touch there in the Dao class

  • If it’s the file ClientesDao.php that is wrong, it is he you need to edit. Either you change the file as Leonardo said, or you do not use the class DateTime.

  • Because you don’t want to change ClientesDAO?

  • can leave, the solution solves the problem. Boiei here, it is not possible to send php Date|Time object to Mysql

Browser other questions tagged

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