Have the following Entity:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* Address
*
* @ApiResource(
* collectionOperations={"get"={"method"="GET"}},
* itemOperations={"get"={"method"="GET"}}
* )
* @ORM\Table(name="address")
* @ORM\Entity
*/
class Address
{
/**
* @return int
*/
public function getId(): int
{
return $this->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;
}
/**</h2>
* @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:
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",
Where is my fault? Using Symfony 4.0.
The getter getLat
has the return type hint string
, 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:
$address = new Address();
// the following lines will produce an error in your example
// because your return type hint doesn't allow null values
$address->getId(); // returns null
$address->getStreet(); // returns null
$address->getLat(); // returns null
$address->setLat("4.56789");
$address->getLat(); // returns "4.56789"
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:
/**
* @return string|null
*/
public function getLat(): ?string
{
return $this->lat;
}
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:
/**
* @var string
*
* @ORM\Column(name="lat", type="decimal", precision=10, scale=8, nullable=false)
*/
private $lat = "";
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:
public function __constructor(string $lat, string $lng /*, add the other required properties */) {
$this->lat = $lat;
$this->lng = $lng;
// ... additional properties here ...
}
In that case you must provide the values when you create the object with new Address(/* parameters go here */);
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With