Я использую Symfony 5.1 (PHP7 .2 ), и проблема в том, что аннотации групп сериализации сущностей не работают.
Вот моя сущность:
namespace App\Entity;
use App\Repository\ArticleRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=ArticleRepository::class)
*/
class Article
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user"})
*/
private $alias;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"user"})
*/
private $title;
/**
* @ORM\Column(type="text")
* @Groups({"user"})
*/
private $content;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @ORM\Column(type="datetime")
*/
private $updated_at;
/**
* @ORM\Column(type="string", length=255)
*/
private $image_link;
/**
* @ORM\Column(type="boolean")
*/
private $appearance_status;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="articles")
* @ORM\JoinColumn(nullable=false)
*/
private $author;
public function getId(): ?int
{
return $this->id;
}
public function getAlias(): ?string
{
return $this->alias;
}
public function setAlias(string $alias): self
{
$this->alias = $alias;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->created_at = $created_at;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updated_at;
}
public function setUpdatedAt(\DateTimeInterface $updated_at): self
{
$this->updated_at = $updated_at;
return $this;
}
public function getImageLink(): ?string
{
return $this->image_link;
}
public function setImageLink(string $image_link): self
{
$this->image_link = $image_link;
return $this;
}
public function getAppearanceStatus(): ?bool
{
return $this->appearance_status;
}
public function setAppearanceStatus(bool $appearance_status): self
{
$this->appearance_status = $appearance_status;
return $this;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): self
{
$this->author = $author;
return $this;
}
}
Вот как я обрабатываю сущность и пытаюсь сериализовать ее содержимое:
/**
* Returns serialized list of articles
*
* @param Article[] $articles
* @return string JSON string
*/
public function serializeArticles(array $articles) : string
{
$dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
return $innerObject instanceof \DateTime ? $innerObject->format(\DateTime::ISO8601) : '';
};
$defaultContext = [
AbstractNormalizer::GROUPS => ['user'],
AbstractNormalizer::CALLBACKS => [
'createdAt' => $dateCallback,
'updatedAt' => $dateCallback
],
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER =>
function ($articles, $format, $context) {
return $articles->getId();
},
AbstractNormalizer::IGNORED_ATTRIBUTES => [
'__initializer__', '__isInitialized__',
'__cloner__'
]
];
$JsonEncoder = new JsonEncoder();
$objectNormalizer =
new ObjectNormalizer(null, null, null,
null, null, null,
$defaultContext)
;
$serializer = new Serializer(
[$objectNormalizer], [$JsonEncoder]
);
$articles = $serializer->serialize($articles, 'json');
return $articles;
}
В любом случае группы сериализации не хотят работать. Черновик выглядит так, будто я не использую группы сериализации.