I have currently this entity and I want to show my property "firedDate" in my JSON even is the value is null.
/**
* @ApiResource(normalizationContext={"groups"={"employee"}})
* @ApiFilter(DateFilter::class, properties={"dateProperty": DateFilter::INCLUDE_NULL_BEFORE_AND_AFTER})
* @ORM\Table(name="employee")
*/
class Employee
{
// ...
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"employee"})
*/
private $firedDate;
public function getFiredDate(): ?\DateTimeInterface
{
return $this->firedDate;
}
// ...
}
What i'm doing wrong? :/ Thanks!
I think I found the right solution to this problem.
Set skip_null_values
in false
in your normalizationContext
:
* @ApiResource(
* itemOperations={
* "get" = {
* //...
* }
* "put" = {
* //...
* },
* "patch" = {
* //...
* }
* },
* collectionOperations={
* "get",
* "post" = {
* //...
* }
* },
* normalizationContext={
* "skip_null_values" = false,
* "groups" = {"object:read"}
* },
* denormalizationContext={"groups" = {"object:write"}}
* )
Are you under PHP 7.0 or above? In PHP 7.1 you can have nullable return types for functions, so your
public function getFiredDate(): ?\DateTime
{
return $this->firedDate;
}
With the ?
before \DateTime, the function will return null
as well.
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