我有JMS序列化程序的問題。當我使用組時,JMS不會序列化我的子類,但是當我不使用組時,一切都可以。我做錯了什麼?JMS序列化程序不序列化子類
$context = SerializationContext::create()->enableMaxDepthChecks();
$context->setGroups(['clientapi']);
$contextWithoutGroup = SerializationContext::create()->enableMaxDepthChecks();
/** @var Serializer $serializer */
$serializer = $this->container->get('jms_serializer');
$dataClientApi = $serializer->serialize($documentBundle->getFolderDocumentsForClientApi(
$this->getUserFromParam($params), $folder, $categories, $tags
), 'json', $context);
$dataWithout = $serializer->serialize($documentBundle->getFolderDocumentsForClientApi(
$this->getUserFromParam($params), $folder, $categories, $tags
), 'json', $$contextWithoutGroup);
給:
$dataClientApi = '{"0":{"author":{}}}';
$dataWithout = '{"0":{"author":{id: 2}}}';
這是我的課。家長:
/**
* Document
*
* @ORM\Table(name="documents")
* @ORM\Entity(repositoryClass="AppBundle\Entity\DocumentRepository")
* @ORM\EntityListeners({"DocumentListener"})
* @JMS\ExclusionPolicy("all")
* @JMS\AccessorOrder("custom", custom = {"id", "folderId", "title"})
*/
class Document implements ResourceInterface
{
use Traits\SortableTrait;
use Traits\TimestampableTrait;
/**
* @var integer
*
* @ORM\Column(type="integer", name="id")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @JMS\Groups({"clientapi"})
* @JMS\Expose()
*/
protected $id;
/**
* @var \AppBundle\Entity\Author
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Author")
* @ORM\JoinColumn(name="author_id", nullable=true, referencedColumnName="id")
* @JMS\Groups({"clientapi"})
* @JMS\MaxDepth(3)
* @JMS\Expose()
*/
protected $author;
而子類:
/**
* Author
*
* @ORM\Entity
* @ORM\Table(name="author")
* @Gedmo\Uploadable(pathMethod="getDirPath", allowOverwrite=false, filenameGenerator="SHA1", appendNumber=true)
* @JMS\ExclusionPolicy("none")
* @JMS\AccessorOrder("custom", custom = {"id"})
*/
class Author implements ResourceInterface, FileInterface
{
const DIR_PATH = 'web/system/authors';
use Traits\FileTrait;
use Traits\TimestampableTrait;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @JMS\Groups({"clientapi"})
* @JMS\Expose()
*/
protected $id;
我不能跟你提供一個乾淨的項目中的代碼重現該問題... – Hammerbot