Symfony2 Doctrine – Catchable Fatal Error: Object of class XYZ could not be converted to string in Doctrine\DBAL\Statement.php line 131
Hello folks,
While developing a Symfony2 Web App you will face a lot of uncommon errors, due to your fault, but even if the error description is often effective, there are times where it’s not enough and even if you manage to get things working, it’s always a better pratice to understand why shit happens.
Today we’re talking about this error:
Catchable Fatal Error: Object of class Bundle\Entity\YourEntity could not be converted to string in \www\symfony\vendor\doctrine-dbal\lib\Doctrine\DBAL\Statement.php line 131
Every Symfony developer have seen it before, the solution is quite easy, you could easily find it on StackOverflow…
Find the Entity the error show
....Object of class Bundle\Entity\YourEntity could not....
This entity probably has an ID field, to resolve it quickly simply add
public function __toString() { return strval($this->id); }
After the variables declarations. Easy as pie.
But why this happened? Other Entity works fine and this thing happens only once in a while.
The problem is not related to the controller you are calling right now, it’s related to repository call you just made, you could have wrote a code similar to mine:
// Repository Call $product_repository = $em->getRepository('Bundle:Product'); $reseller_repository = $em->getRepository('Bundle:Reseller'); // Get a random product, for test reason $product = new Product(); // Entity Product $id = 1; // Random Item ID $product = $product_repository ->findOneById($id); // All fine till now, doing random things for next steps // Get a reseller having the Product, assume it's a ManyToOne or something logical $reseller = new Reseller(); // Entity Reseller $reseller = $reseller_repository ->findOneByproduct($product); // HERE IS THE PROBLEM!
The error is due to your try to get a Reseller from a Product Entity, but even if the Reseller Entity has got the method $reseller->getProduct() the Doctrine orm is not smart enough to know that the relation is linked by the ID, so declaring a __toString() method helps him to get the ID from the Entity itself.
Ok my explanation is not quite technical, there a lot of misteries even for me, but i at least got the cause of this unusual but nasty error.
That’s all men, thanks for reading and GL from now on!
you are amazing man! i had this problem on ZF2 and nowbody is asking/talking about this issue there!
Thanks for sharing!!!
Glad to help π
I would say thanks to you…after 2 days of smashin my head I found this simple and clear explanation of my probs! big up!
Don’t worry, i made this post because i wasted a lot of time too, trying to get the fix. You’re welcome π
thx a lot man <3 <3
Thank you, really! You saved my day!
Great
Thank you!!! you’ve fixed my problem!!
hell yeah!!!! THANK YOU….
You are the man!!!! Thanks!!!!
I was just banging my head on the desk, thank god i found this. Weird this problem occur, but thank you for this tutorial.
Thanks… very thanks
Thanksβ¦ very thanks π
Thank you! Nothing to say except … thank you!
Thanku so much… π
im using symfony 4 and i couldnt solve this fatal problem anyone help me please