Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine Many To One Mapping Issue

I am getting the following error in my "Create Product" page when trying to create a foreign key mapping (i.e mapping Category to Product):

A "__toString()" method was not found on the objects of type "CJ\BusinessBundle\Entity\Category" passed to the choice field. To read a custom getter instead, set the option "property" to the desired property path.

like image 701
chirag7jain Avatar asked Mar 19 '13 10:03

chirag7jain


2 Answers

You need to add a __toString() method to your Category entity. For example:

public function __toString()
{
    return $this->name;
}

The PHP magic-method __toString() is used to present a textual representation of the object. In this case, the Category name will be used when selecting a Category in a Form of a related entity.

like image 126
Juan Sosa Avatar answered Oct 10 '22 04:10

Juan Sosa


The error message is telling you what you need to do. In your Category entity you need to add a __toString() method so that when you add a product it knows what to name each item in the select box on the form.

public function __toString()
{
    return $this->name;
}

In the above replace 'name' with whichever field is the readable identifier for your category.

like image 33
Mark Avatar answered Oct 10 '22 05:10

Mark