Have the following Entity:
id;
}
/**
* @return string
*/
public function getLat(): string
{
return $this->lat;
}
/**
* @param string $lat
*/
public function setLat(string $lat): void
{
$this->lat = $lat;
}
/**
* @return string
*/
public function getLng(): string
{
return $this->lng;
}
/**
* @param string $lng
*/
public function setLng(string $lng): void
{
$this->lng = $lng;
}
/**
* @return string
*/
public function getStreet(): string
{
return $this->street;
}
/**
* @param string $street
*/
public function setStreet(string $street): void
{
$this->street = $street;
}
/**
* @return string
*/
public function getZipcode(): string
{
return $this->zipcode;
}
/**
* @param string $zipcode
*/
public function setZipcode(string $zipcode): void
{
$this->zipcode = $zipcode;
}
/**
* @return string
*/
public function getCity(): string
{
return $this->city;
}
/**
* @param string $city
*/
public function setCity(string $city): void
{
$this->city = $city;
}
/**
* @return string
*/
public function getDescription(): string
{
return $this->description;
}
/**
* @param string $description
*/
public function setDescription(string $description): void
{
$this->description = $description;
}
/**
* @var string
*
* @ORM\Column(name="lat", type="decimal", precision=10, scale=8, nullable=false)
*/
private $lat;
/**
* @var string
*
* @ORM\Column(name="lng", type="decimal", precision=10, scale=8, nullable=false)
*/
private $lng;
/**
* @var string
*
* @ORM\Column(name="street", type="string", length=255, nullable=false)
*/
private $street;
/**
* @var string
*
* @ORM\Column(name="zipcode", type="string", length=5, nullable=false)
*/
private $zipcode;
/**
* @var string
*
* @ORM\Column(name="city", type="string", length=255, nullable=false)
*/
private $city;
/**
* @var string
*
* @ORM\Column(name="description", type="text", nullable=false)
*/
private $description;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
}
The table with one row of data:
[![enter image description here][1]][1]
[![enter image description here][2]][2]
I got this error:
>> "type": "https://www.rfc-editor.org/rfc/rfc2616#section-10",
"title": "An error occurred",
"detail": "Type error: Return value of App\\Entity\\Address::getLat() must be of the type string, null returned",
[![enter image description here][3]][3]
Where is my fault? Using Symfony 4.0.
[1]: https://i.stack.imgur.com/OQw4b.png
[2]: https://i.stack.imgur.com/HrFT3.png
[3]: https://i.stack.imgur.com/PCPww.png
Best Answer
The getter
getLat
has the return type hintstring
, which means only actual string values (that also means: no null values!) as return values are accepted.You didn't show the code where you actually work with the entity, but it basically comes down to the default value of every property in an object being
null
if not defined differently.Look at this example:
Note regarding Doctrine:
If the values in the database are set correctly, you won't run into this problem after Doctrine populated the entity (e.g. via
$address = $addressRepo->find(123);
). It should only happen, when you create a new entity yourself and then try to use the getter methods.Possible solutions:
1.) Allow null values as return values. Prepend your return type hints with a question mark, like this:
But if you do this, your code must be ready to handle null values from these methods!
2.) Define default values with the correct data type in your object:
Your code must be ready to handle empty strings as return values then! Alternativly you can also define the default values in a constructor method.
3.) Require these properties to be available by making them parameters of your constructor:
In that case you must provide the values when you create the object with
new Address(/* parameters go here */);
.