I am completely lost here... After validating some input I create an instance of a Message
class and attempt to insert data into the database:
// send message
$message = $this->model->build('message', true);
$message->insertMessage($uid, $user->user_id, $title, $message);
The method for insertion is very straight-forward:
// insert a new message
public function insertMessage($to_id, $from_id, $title, $body)
{
$sql = "INSERT INTO messages (to_id, from_id, title, body, create_date) VALUES
(:to_id, :from_id, :title, :body, NOW())";
$sth = $this->db->prepare($sql);
return $sth->execute([':to_id' => $to_id, ':from_id' => $from_id, ':title' => $title, ':body' => $body]);
}
However, upon submission I end up with a blank page and the Apache error log says:
[Tue Jul 30 22:34:44 2013] [error] [client 127.0.0.1] PHP Catchable fatal error: Object of class framework\models\Message could not be converted to string in /var/www/p-lug/p-lug_lib/framework/models/Message.php on line 18, referer: https://p-lug.localhost/message/compose/4
Line 18 refers to the return statement, but even if I remove return
it results in the same error.
I've read countless links regarding this error but none of the answers appear to apply to my example. At no point am I trying to convert an object to a string or output the result, and similar code for insertions with other classes works perfectly. In fact, this code was copy-pasted from another working example, the only thing changed is the table and data.
I've used var_dump()
on all the variables being passed, on $this
and $sth
, everything checks out. But on execute()
it fails. What the heck is going on here?
So $message contains an object.
This object gets passed to the function insertMessage as the 4th argument ($body which is still the same object)
You then store the Message object stored in the variable $body in the hash array which is passed as an argument to execute.
The execute function attempts to convert the Message object to a string but finds that there is not __toString function declared.
So either declare
public function __toString() {
return $the_string;
}
or create another public function/member that you can pass to the execute function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With