4

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?

Raptor
  • 53,206
  • 45
  • 230
  • 366
mister martin
  • 6,197
  • 4
  • 30
  • 63
  • What does `Message` class have? Could you post that? – vee Jul 31 '13 at 04:01
  • *but even if I remove return it results in the same error.* that means the error is on the line above. Check if `$this->db` is valid. – Raptor Jul 31 '13 at 04:02

2 Answers2

15

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.

Logan Murphy
  • 6,120
  • 3
  • 24
  • 42
  • You're right. I defined `$message` from `_POST`, and then inadvertently redefined it when creating the object and passed it to itself. Not the first time I've mixed up variables like that either, damn it. Thank you! – mister martin Jul 31 '13 at 04:13
  • Right that is exactly what I meant to say lol. I thought that passing the objects reference to itself (more or less) was kind of weird. :P – Logan Murphy Jul 31 '13 at 13:55
0

$message which is passed to the $body argument is an object returned from your build() method in the model.

You can simply return the message that was built in the build() method rather than an object.

spenibus
  • 4,339
  • 11
  • 26
  • 35
SGAmpere
  • 21
  • 3