自引用实体的序列化

时间:2019-07-04 06:51:02

标签: php symfony api-platform.com

我想知道是否有可能在嵌入式实体的GET操作中显示不同的属性。

这是我的课程:

/**
 * @ORM\Table(name="folders")
 * @ORM\Entity(repositoryClass="App\Repository\FolderRepository")
 */
class Folder
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="Folder", inversedBy="childFolders", cascade={"persist"})
     * @ORM\JoinColumn(name="folder_parent", referencedColumnName="id")
     */
    private $parentFolder;

    /**
     * @ORM\OneToMany(targetEntity="Folder", mappedBy="parentFolder", cascade={"persist"})
     */
    private $childFolders;

    /**
     * @ORM\OneToMany(targetEntity="File", mappedBy="folder", cascade={"persist"})
     * @ORM\JoinColumn(name="id_folder", referencedColumnName="id")
     */
    private $files;
}

我想要得到的结果:

{
    "@context": "\/contexts\/Folder",
    "@id": "\/api\/folders\/561",
    "@type": "Folder",
    "id": 561,
    "name": "SOURCE(6)",
    "parentFolder": {
        "@id": "\/api\/folders\/52",
        "@type": "Folder",
        "id": 52,
        "name": "ashirama workspace",
        "parentFolder": null
     },
     "files": [
        {
            "name": "file(1).png"
        }
      ]
}

实际结果:

{
    "@context": "\/contexts\/Folder",
    "@id": "\/api\/folders\/561",
    "@type": "Folder",
    "id": 561,
    "name": "SOURCE(6)",
    "parentFolder": {
        "@id": "\/api\/folders\/52",
        "@type": "Folder",
        "id": 52,
        "name": "ashirama workspace",
        "parentFolder": null,
        "files": [
              {
                   "name": "file(1).png"
              }
         ]
      },
     "files": [
        {
            "name": "file(2).png"
        }
     ]
}

我只想在实际文件夹上保存文件。 我认为有些事情我不理解,但是也许有一个关于该用例的已知方法。

谢谢。

0 个答案:

没有答案