Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Doctrine mapped superclasses #1395

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Metadata/Driver/AbstractDoctrineTypeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
103 changes: 103 additions & 0 deletions tests/Fixtures/Doctrine/Entity/AbstractBlogPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures\Doctrine\Entity;

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;

/**
* @ORM\MappedSuperclass
*/
abstract class AbstractBlogPost
{
/**
* @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\Column(type="guid")
*/
private $guid;

/**
* @ORM\Column(type="string")
*
* @Groups({"post"})
*/
#[Groups(groups: ['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\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;
$this->createdAt = $createdAt;
}

public function setPublished()
{
$this->published = true;
}

/**
* @Serializer\VirtualProperty()
*/
#[Serializer\VirtualProperty]
public function getRef()
{
return $this->ref;
}
}
88 changes: 2 additions & 86 deletions tests/Fixtures/Doctrine/Entity/BlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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")
*
Expand All @@ -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;
}
}