Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8946725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:35:06+00:00 2026-06-15T12:35:06+00:00

I’m trying to code a class handling serialization of documents by reading their metadata.

  • 0

I’m trying to code a class handling serialization of documents by reading their metadata. I got inspired by this implementation for entities with Doctrine ORM and modified it to match how Doctrine ODM handles documents. Unfortunatly something is not working correctly as one document is never serialized more than once even if it is refered a 2nd time thus resulting on incomplete serialization.

For example, it outputs this (in json) for a user1 (see User document) that belongs to some place1 (see Place document). Then it outputs the place and the users belonging to it where we should see the user1 again but we don’t :

{
  id: "505cac0d6803fa1e15000004",
  login: "user1",
  places: [
    {
      id: "505cac0d6803fa1e15000005",
      code: "place1",
      users: [
        {
          id: "505c862c6803fa6812000000",
          login: "user2"
        }
      ]
    }
  ]
}

I guess it could be related to something preventing circular references but is there a way around it ?

Also, i’m using this in a ZF2 application, would there be a better way to implement this using the ZF2 Serializer ?

Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:35:07+00:00Added an answer on June 15, 2026 at 12:35 pm

    I have a serializer already written for DoctrineODM. You can find it in http://github.com/superdweebie/DoctrineExtensions – look in lib/Sds/DoctrineExtensions/Serializer.

    If you are are using zf2, then you might also like http://github.com/superdweebie/DoctrineExtensionsModule, which configures DoctrineExtensions for use in zf2.

    To use the Module, install it with composer, as you would any other module. Then add the following to your zf2 config:

    'sds' => [
        'doctrineExtensions' => [
            'extensionConfigs' => [
                'Sds\DoctrineExtensions\Serializer' => null,
            ),
        ),
    ),
    

    To get the serializer use:

    $serializer = $serivceLocator->get('Sds\DoctrineExtensions\Serializer');
    

    To use the serializer:

    $array = $serializer->toArray($document)
    $json = $serializer->toJson($document)
    
    $document = $serializer->fromArray($array)
    $document = $serializer->fromJson($json)
    

    There are also some extra annotations available to control serialization, if you want to use them:

    @Sds\Setter - specify a non standard setter for a property
    @Sds\Getter - specify a non standard getter fora  property
    @Sds\Serializer(@Sds\Ignore) - ignore a property when serializing
    

    It’s all still a work in progress, so any comments/improvements would be much appreciated. As you come across issues with these libs, just log them on github and they will get addressed promptly.

    Finally a note on serializing embedded documents and referenced documents – embedded documents should be serialized with their parent, while referenced documents should not. This reflects the way data is saved in the db. It also means circular references are not a problem.

    Update

    I’ve pushed updates to Sds/DoctrineExtensions/Serializer so that it can now handle references properly. The following three (five) methods have been updated:

    toArray/toJson
    fromArray/fromJson
    applySerializeMetadataToArray
    

    The first two are self explainitory – the last is to allow serialization rules to be applied without having to hydrate db results into documents.

    By default references will be serialized to an array like this:

    [$ref: 'CollectionName/DocumentId']
    

    The $ref style of referencing is what Mongo uses internally, so it seemed appropriate. The format of the reference is given with the expectation it could be used as a URL to a REST API.

    The default behaviour can be overridden by defineing an alternative ReferenceSerializer like this:

    /**
     * @ODM\ReferenceMany(targetDocument="MyTargetDocument")
     * @Sds\Serializer(@Sds\ReferenceSerializer('MyAlternativeSerializer'))
     */
    protected $myDocumentProperty;
    

    One alternate ReferenceSerializer is already included with the lib. It is the eager serializer – it will serialize references as if they were embedded documents. It can be used like this:

    /**
     * @ODM\ReferenceMany(targetDocument="MyTargetDocument")
     * @Sds\Serializer(@Sds\ReferenceSerializer('Sds\DoctrineExtensions\Serializer\Reference\Eager'))
     */
    protected $myDocumentProperty;
    

    Or an alternate shorthand annotation is provided:

    /**
     * @ODM\ReferenceMany(targetDocument="MyTargetDocument")
     * @Sds\Serializer(@Sds\Eager))
     */
    protected $myDocumentProperty;
    

    Alternate ReferenceSerializers must implement Sds\DoctrineExtensions\Serializer\Reference\ReferenceSerializerInterface

    Also, I cleaned up the ignore annotation, so the following annotations can be added to properties to give more fine grained control of serialization:

    @Sds\Serializer(@Sds\Ignore('ignore_when_serializing'))
    @Sds\Serializer(@Sds\Ignore('ignore_when_unserializing'))
    @Sds\Serializer(@Sds\Ignore('ignore_always'))
    @Sds\Serializer(@Sds\Ignore('ignore_never'))
    

    For example, put @Sds\Serializer(@Sds\Ignore('ignore_when_serializing')) on an email property – it means that the email can be sent upto the server for update, but can never be serialized down to the client for security.

    And lastly, if you hadn’t noticed, sds annotations support inheritance and overriding, so they play nice with complex document structures.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.