I am getting an object from an API of Twilio. To be more specific, I am using the following function https://www.twilio.com/docs/conversations/api/conversation-participant-resource
When I try to insert de datetime objects to mysql I receive the error explained on the subject of this post.
Also, I tested change the fields type to set as date, datetime, text or varchar but nothing works.
Can somebody give me a clue? Any help is much appreaciated Thanks
Model:
$sql=[
"id_conversation"=>$conversation_id,
"identity"=>$record->identity,
"identity_proxy_address"=>$explode_whatsapp[1],
"date_created"=>$record->dateCreated,
"date_updated"=>$record->dateUpdated,
];
$builder->insert($sql);
Another way tried too:
$begin = new \DateTime($record->dateCreated);
error: DateTime::__construct() expects parameter 1 to be string, object given
CodePudding user response:
What that error says is that it can't use an instance of DateTime
as a string, which is unrelated to your database column types or how you instantiate the DateTime
instance.
Since $record->dateCreated
and $record->dateUpdated
already are instances of DateTime
, you need to get the dates as strings:
"date_created" => $record->dateCreated->format('Y-m-d H:i:s'),
"date_updated" => $record->dateUpdated->format('Y-m-d H:i:s'),
(And keep your database column types here as datetime
)