symfony2 Catchable Fatal Error: Object of class could not be converted to string

symfony2 Catchable Fatal Error: Object of class could not be converted to string

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Catchable Fatal Error: Object of class could not be converted to string

You are encountering a Catchable Fatal Error because Doctrine is attempting to map an object onto a scalar database column, and the mapping configuration is fundamentally broken. The error message explicitly states that an instance of Apw\BlackbullBundle\Entity\CategoriesDescription cannot be converted to a string where a string is expected. This happens during the execution of your INSERT statement because the query builder generated by Doctrine is looking for a scalar value (like a UUID or slug) in the categories_image column, but it is finding an entire object.

The root cause lies in the CategoriesType.php form configuration within the buildForm method. You have configured the parentId field like this:

->add('parentId', 'entity', array( 
    'class'       => 'ApwBlackbullBundle:CategoriesDescription',
    'property'    => 'categoriesName',
    // ...
))

Here, you are telling Symfony to create an entity field for parentId that queries the CategoriesDescription entity. However, your controller and entity definition tell a completely different story. In your Categories entity, the $parentId field is defined as an integer:

/**
 * @var integer
 * @ORM\Column(name="parent_id", type="integer")
 */
private $parentId;

There is a direct contradiction here. The form thinks it is managing a relationship to CategoriesDescription, but the database schema expects an integer ID for the parent_id. Furthermore, you are persisting both the main category and the description manually using $em->persist($category) and $em->persist($categoryDesc). When Doctrine flushes this state, it tries to hydrate the entity based on the form field mapping. Since the form field parentId is mapped to a collection or a different entity type than what exists in the database schema for that specific column, the internal representation gets mangled, leading to the scalar conversion failure when Doctrine attempts to bind parameters.

To fix this, you need to align your form configuration with your actual data model. If parent_id is intended to reference another category (a self-referential many-to-one relationship), it should be mapped to an integer or a specific entity that holds the ID, not CategoriesDescription.

If you intend for the user to select a parent category from a dropdown, the form field should look something like this:

->add('parentId', 'entity', array( 
    'class'       => 'Apw\BlackbullBundle\Entity\Categories', // Corrected entity class
    'property'    => 'id',                                 // Property to display as label
    'mapped'      => false,                                // Do not map this to a column if it's optional logic
    'required'    => false,
    'label'       => 'Parent Category',
))

Alternatively, if you are strictly using an integer ID for the parent, ensure the form field type is integer and you manually provide the choices or handle the lookup logic in the controller before passing data to the form.

Additionally, there is a significant issue with how you are handling relationships in your controller. You instantiate $categoryDesc and link it to $category, then persist both. However, your Categories entity uses a cascade configuration:

@ORM\OneToMany(targetEntity="CategoriesDescription", mappedBy="category", cascade={"persist", "remove"})

Because you have configured cascading persistence on the relationship, Doctrine expects to manage the child entities automatically when you persist the parent. By explicitly calling $em->persist($categoryDesc) separately, you are creating a potential inconsistency in the object graph that can lead to data integrity issues or unexpected mapping errors during hydration. You should rely on Doctrine's cascade settings and only persist the root entity ($category).

Finally, regarding the categories_image column: your form definition shows 'add' => 'Foto:'. The error log indicates a parameter of "as" was passed where an image path or slug is expected. This suggests that the parentId field mapping in the form is somehow bleeding into the wrong column or that the form is misinterpreting the object type entirely due to the incorrect class definition provided earlier. Correcting the entity class in the form builder to point to Categories instead of CategoriesDescription for the parent field

Our network