chirag7jain
chirag7jain

Reputation: 1547

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.

Upvotes: 10

Views: 8388

Answers (2)

Juan Sosa
Juan Sosa

Reputation: 5280

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.

Upvotes: 29

Mark
Mark

Reputation: 1754

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.

Upvotes: 6

Related Questions