ManyToOne序列化

时间:2014-11-05 13:14:56

标签: symfony jmsserializerbundle

我有序列化ManyToOne关系的问题。 我的家庭成员有自己或其他家庭成员写的关于他的笔记:

/**
 * FamilyMember
 *
 * @ORM\Table(name="familymember")
 * @ORM\Entity(repositoryClass="***\MainBundle\Entity\FamilyMemberRepository")
 * @ExclusionPolicy("all")
 */
class FamilyMember
{
[*************]

    /**
     * @ORM\OneToMany(targetEntity="***\MainBundle\Entity\Note", mappedBy="familyMember")
     * @Type("ArrayCollection<***\MainBundle\Entity\Note>")
     * @Expose
     * @Groups({"getFamilies"})
     */
    protected $notes;

    /**
     * @ORM\OneToMany(targetEntity="***\MainBundle\Entity\Note", mappedBy="author")
     */
    protected $notesByMember;



/**
 * Note
 *
 * @ORM\Table(name="note")
 * @ORM\Entity(repositoryClass="***\MainBundle\Entity\NoteRepository")
 * @ExclusionPolicy("all")
 */

class Note
{
[******]

    /**
     * @ORM\ManyToOne(targetEntity="***\MainBundle\Entity\FamilyMember", inversedBy="notes",  cascade={"persist"})
     * @ORM\JoinColumn(name="familyMember_id", referencedColumnName="id", nullable=true)
     * @Expose
     * @Groups({"getFamilies"})
     * @Type("***\MainBundle\Entity\FamilyMember")
     * @MaxDepth(3)
     * @SerializedName("familyMember")
     */
    protected $familyMember;

    /**
     * @ORM\ManyToOne(targetEntity="***\MainBundle\Entity\FamilyMember", inversedBy="notesByMember",  cascade={"persist"})
     * @ORM\JoinColumn(name="author_fm_id", referencedColumnName="id", nullable=true)
     * @Type("***\MainBundle\Entity\FamilyMember")
     * @Expose
     * @Groups({"getFamilies"})
     * @SerializedName("author")
     */
    protected $author;

我试图序列化FamilyMember,我需要有关于他的所有笔记以及作为该笔记作者的信息。但是JMS序列化器不能序列化我的ManyToOne关系。有人能帮助我吗?

编辑:

序列化结果

{  
 "families":[  
  {  
     "id":1,
     "name":"nowa",
     "familyMembers":[  
        {  
           "id":1,
           "familyMemberType":1,
           "user":{  

           },
           "name":"test",
           "isAdmin":true,
           "notes":[  
              {  
                 "id":1,
                 "text":"cvzzs",
                 "isVisible":false
              }
           ],
           "email":"test@test.pl",
           "parents":[  

           ],
           "children":[  
              {  
                 "id":3,
                 "familyMemberType":6,
                 "name":"dziecko",
                 "isAdmin":false,
                 "notes":[  

                 ],
                 "email":"",
                 "phoneNumber":"",
                 "parents":[  

                 ],
                 "children":[  

                 ]
              }
           ]
        },
        {  
           "id":2,
           "familyMemberType":2,
           "name":"mama",
           "isAdmin":false,
           "notes":[  

           ],
           "email":"",
           "phoneNumber":"",
           "parents":[  

           ],
           "children":[  
              {  
                 "id":3,
                 "familyMemberType":6,
                 "name":"dziecko",
                 "isAdmin":false,
                 "notes":[  

                 ],
                 "email":"",
                 "phoneNumber":"",
                 "parents":[  

                 ],
                 "children":[  

                 ]
              }
           ]
        },
        {  
           "id":3,
           "familyMemberType":6,
           "name":"dziecko",
           "isAdmin":false,
           "notes":[  

           ],
           "email":"",
           "phoneNumber":"",
           "parents":[  
              {  
                 "id":1,
                 "familyMemberType":1,
                 "name":"test",
                 "isAdmin":true,
                 "notes":[  

                 ],
                 "email":"test@test.pl",
                 "parents":[  

                 ],
                 "children":[  

                 ]
              },
              {  
                 "id":2,
                 "familyMemberType":2,
                 "name":"mama",
                 "isAdmin":false,
                 "notes":[  

                 ],
                 "email":"",
                 "phoneNumber":"",
                 "parents":[  

                 ],
                 "children":[  

                 ]
              }
           ],
           "children":[  

           ]
        }
     ],
     "isGroup":false
  }
]
}

编辑2: 几分钟前,我已向其他家庭成员添加了第二个注释:

{
    "families": [
        {
            "id": 1,
            "name": "nowa",
            "familyMembers": [
                {
                    "id": 1,
                    "familyMemberType": 1,
                    "user": {},
                    "name": "test",
                    "isAdmin": true,
                    "notes": [
                        {
                            "id": 1,
                            "text": "cvzzs",
                            "isVisible": false
                        }
                    ],
                    "email": "test@test.pl",
                    "parents": [],
                    "children": [
                        {
                            "id": 3,
                            "familyMemberType": 6,
                            "name": "dziecko",
                            "isAdmin": false,
                            "notes": [],
                            "email": "",
                            "phoneNumber": "",
                            "parents": [],
                            "children": []
                        }
                    ]
                },
                {
                    "id": 2,
                    "familyMemberType": 2,
                    "name": "mama",
                    "isAdmin": false,
                    "notes": [
                        {
                            "id": 2,
                            "text": "gowno",
                            "isVisible": true,
                            "author": {
                                "id": 1,
                                "familyMemberType": 1,
                                "user": {},
                                "name": "test",
                                "isAdmin": true,
                                "notes": [
                                    {
                                        "id": 1,
                                        "text": "cvzzs",
                                        "isVisible": false
                                    }
                                ],
                                "email": "test@test.pl",
                                "parents": [],
                                "children": [
                                    {}
                                ]
                            }
                        }
                    ],
                    "email": "",
                    "phoneNumber": "",
                    "parents": [],
                    "children": [
                        {
                            "id": 3,
                            "familyMemberType": 6,
                            "name": "dziecko",
                            "isAdmin": false,
                            "notes": [],
                            "email": "",
                            "phoneNumber": "",
                            "parents": [],
                            "children": []
                        }
                    ]
                },
                {
                    "id": 3,
                    "familyMemberType": 6,
                    "name": "dziecko",
                    "isAdmin": false,
                    "notes": [],
                    "email": "",
                    "phoneNumber": "",
                    "parents": [
                        {
                            "id": 1,
                            "familyMemberType": 1,
                            "name": "test",
                            "isAdmin": true,
                            "notes": [],
                            "email": "test@test.pl",
                            "parents": [],
                            "children": []
                        },
                        {
                            "id": 2,
                            "familyMemberType": 2,
                            "name": "mama",
                            "isAdmin": false,
                            "notes": [],
                            "email": "",
                            "phoneNumber": "",
                            "parents": [],
                            "children": []
                        }
                    ],
                    "children": []
                }
            ],
            "isGroup": false
        }
    ]
}

正如您所见,ID为2的家庭成员有一个作者字段。因此,如果作者不同于所提交的注释的所有者是序列化的。我已经添加到$ author maxDepth 3(我认为有某种inifity循环)但这没有帮助

0 个答案:

没有答案