From ff1ba1876b80041489819c9b7e4c39e7177236c8 Mon Sep 17 00:00:00 2001 From: Dominic Tubach <5405481+dontub@users.noreply.github.com> Date: Wed, 9 Mar 2022 13:04:53 +0100 Subject: [PATCH] Add support for Doctrine mapped superclasses --- .../Driver/AbstractDoctrineTypeDriver.php | 6 + .../Doctrine/Entity/AbstractBlogPost.php | 103 ++++++++++++++++++ tests/Fixtures/Doctrine/Entity/BlogPost.php | 88 +-------------- 3 files changed, 111 insertions(+), 86 deletions(-) create mode 100644 tests/Fixtures/Doctrine/Entity/AbstractBlogPost.php diff --git a/src/Metadata/Driver/AbstractDoctrineTypeDriver.php b/src/Metadata/Driver/AbstractDoctrineTypeDriver.php index f7d85ea59..b6a18bdd3 100644 --- a/src/Metadata/Driver/AbstractDoctrineTypeDriver.php +++ b/src/Metadata/Driver/AbstractDoctrineTypeDriver.php @@ -107,6 +107,12 @@ public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadat $this->setPropertyType($doctrineMetadata, $propertyMetadata); } + // Add fields from mapped superclasses + if ($parentClass = $doctrineMetadata->getReflectionClass()->getParentClass()) { + $parentClassMetadata = $this->loadMetadataForClass($parentClass); + $classMetadata->propertyMetadata = array_merge($parentClassMetadata->propertyMetadata, $classMetadata->propertyMetadata); + } + return $classMetadata; } diff --git a/tests/Fixtures/Doctrine/Entity/AbstractBlogPost.php b/tests/Fixtures/Doctrine/Entity/AbstractBlogPost.php new file mode 100644 index 000000000..90693731e --- /dev/null +++ b/tests/Fixtures/Doctrine/Entity/AbstractBlogPost.php @@ -0,0 +1,103 @@ +title = $title; + $this->author = $author; + $this->published = false; + $this->createdAt = $createdAt; + } + + public function setPublished() + { + $this->published = true; + } + + /** + * @Serializer\VirtualProperty() + */ + #[Serializer\VirtualProperty] + public function getRef() + { + return $this->ref; + } +} diff --git a/tests/Fixtures/Doctrine/Entity/BlogPost.php b/tests/Fixtures/Doctrine/Entity/BlogPost.php index 43ffcb5c6..7f3088466 100644 --- a/tests/Fixtures/Doctrine/Entity/BlogPost.php +++ b/tests/Fixtures/Doctrine/Entity/BlogPost.php @@ -6,11 +6,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; -use JMS\Serializer\Annotation as Serializer; use JMS\Serializer\Annotation\Groups; -use JMS\Serializer\Annotation\SerializedName; -use JMS\Serializer\Annotation\Type; -use JMS\Serializer\Annotation\XmlAttribute; use JMS\Serializer\Annotation\XmlList; use JMS\Serializer\Annotation\XmlRoot; @@ -20,56 +16,8 @@ * @XmlRoot("blog-post") */ #[XmlRoot(name: 'blog-post')] -class BlogPost +class BlogPost extends AbstractBlogPost { - /** - * @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue(strategy="AUTO") - */ - protected $id; - - /** - * @ORM\Column(type="guid") - */ - private $guid; - - /** - * @ORM\Column(type="string") - * - * @Groups({"comments","post"}) - */ - #[Groups(groups: ['comments', 'post'])] - private $title; - - /** - * @ORM\Column(type="some_custom_type") - */ - protected $slug; - - /** - * @ORM\Column(type="datetime") - * - * @XmlAttribute - */ - #[XmlAttribute] - private $createdAt; - - /** - * @ORM\Column(type="boolean") - * - * @Type("integer") - * This boolean to integer conversion is one of the few changes between this - * and the standard BlogPost class. It's used to test the override behavior - * of the DoctrineTypeDriver so notice it, but please don't change it. - * @SerializedName("is_published") - * @Groups({"post"}) - * @XmlAttribute - */ - #[Type(name: 'integer')] - #[SerializedName(name: 'is_published')] - #[Groups(groups: ['post'])] - #[XmlAttribute] - private $published; - /** * @ORM\OneToMany(targetEntity="Comment", mappedBy="blogPost") * @@ -80,46 +28,14 @@ class BlogPost #[Groups(groups: ['comments'])] private $comments; - /** - * @ORM\OneToOne(targetEntity="Author") - * - * @Groups({"post"}) - */ - #[Groups(groups: ['post'])] - private $author; - - /** - * @Serializer\Exclude() - * @ORM\Column(type="integer") - */ - #[Serializer\Exclude] - private $ref; - public function __construct($title, Author $author, \DateTime $createdAt) { - $this->title = $title; - $this->author = $author; - $this->published = false; + parent::__construct($title, $author, $createdAt); $this->comments = new ArrayCollection(); - $this->createdAt = $createdAt; - } - - public function setPublished() - { - $this->published = true; } public function addComment(Comment $comment) { $this->comments->add($comment); } - - /** - * @Serializer\VirtualProperty() - */ - #[Serializer\VirtualProperty] - public function getRef() - { - return $this->ref; - } }