-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?
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
– Carlos Rocha
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 classDateTime
.– Woss
Because you don’t want to change
ClientesDAO
?– Leonardo Barros
can leave, the solution solves the problem. Boiei here, it is not possible to send php Date|Time object to Mysql
– Carlos Rocha