I'm have this address form. Everything is working as expected, until I added a drop down selection field for the cities. I'm getting this error when I try to save the form:
Catchable Fatal Error: Object of class AppBundle\Entity\Address could not be converted to string
The docs say that the EntityType field is designed to load options from a Doctrine entity.
This is the form:
$builder
->add('cities', EntityType::class, array(
'class' => \AppBundle\Entity\Cities::class,
'choice_label' => 'cityname',
'choice_value' => 'cityid')
)
And this is my entity
/**
* @var string
*
* @ORM\Column(name="cities", type="string", length=55, nullable=false)
*/
private $cities;
And setters/getters:
/**
* Set cities
*
* @param string $cities
*
* @return Address
*/
public function setCities($cities)
{
$this->cities = $cities;
return $this;
}
/**
* Get cities
*
* @return string
*/
public function getCities()
{
return $this->cities;
}
I also added this:
public function __toString() {
return $this->getCities();
}
And had this result:
Catchable Fatal Error: Method AppBundle\Entity\Address::__toString() must return a string value
You should implement __toString()
method for you Entity:
__toString() allows a class to decide how it will react when it is treated like a string. For example, what echo $obj; will print. This method must return a string, as otherwise a fatal E_RECOVERABLE_ERROR level error is emitted.
For example, you could write something like this to represent your class as a string:
public function __toString() {
return $this->getCities();
}
More info about this magic method here.