Learn how to deal with the object could not be converted to string error in symfony >= 2.8.

If you're working with the automatically generation of entities from an existing database, you may want to create the CRUD automatically using the doctrine:generate:crud command from symfony.

Symfony and doctrine allow you to speed up your development time with an automatized way to create routes and forms from a previous generate entity from a table of the database. This function comes really in handy, however it is not perfect and there may be errors during the development.

One of those known problems is the Class couldn't be converted to string :

Catchable Fatal Error: Object of class myBundle\Entity\myClass could not be converted to string

500 Internal Server Error - ContextErrorException

This problem is caused probably, as said before if you use the automatization of forms using the crud command, by your own entities that cannot be converted to string explicitly. "Pretty clear and easy to understand" isn't ?.

What does that means and how do i fix it

Symfony

If you're new in the implementation, you'll probably don't have the most remote idea of how to solve it, but don't worry you'll be fixing it in a couple of seconds and understand why it happens.

Consider the following example and foreign relations :

Category Subcategory
id id
name name
category_id

A subcategory register requires as one of its field a category_id which can be only obtained from the category table, a simple foreign key relation.

But that's not the main point, the main point is how the automatic crud will generate the form ! That's when the error occurs. The form will be created perfectly and theoretically should look like:

Catchable fatal error

However it will not work because the select element of the Subcategory form cannot be rendered and it will throw the catchable fatal error exception because the Category entity file doesn't know which field should be displayed in the select and it doesn't provide neither the __toString() magic method to target manually a field.

Therefore, if we go to the Category (or the class withe the problem in your project) entity file in the bundle, the class should look like :

<?php

namespace myBundle\Entity;

/**
 * Category
 */
class Category
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    private $description;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Category
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set description
     *
     * @param string $description
     *
     * @return Category
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Generates the magic method
     * 
     */
    public function __toString(){
        // to show the name of the Category in the select
        return $this->name;
        // to show the id of the Category in the select
        // return $this->id;
    }
}

To solve the issue, add the __toString method to the class that needs to be shown in the Select input, in this case the Category entity as shown in the previous snippet.

Now try to load the route where the error was thrown and see how the select input is rendered correctly, have fun !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors