DDC-3183: Add JsonSerializable to Collections #3965

doctrinebot opened this Issue Jun 22, 2014 · 5 comments

4 participants


Jira issue originally created by user gabrielbull:

Implement this:


Can't really make this claim if Doctrine is not implementing basic interfaces for collections:

The missing (SPL) Collection/Array/OrderedMap interface.

@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot added this to the 3.0 milestone Dec 6, 2015

I also would like this, would make some use-cases slightly simpler :) Also, as far as I can imagine, all that is needed is this function in ArrayCollection:

function jsonSerialize()
    return $this->elements;

Obvously this would require all elements in the array to also be json serializable... Should I try to create a pull-request for this, and if so, add it sooner that v. 3?

Doctrine member

This is a basic SRP violation. Serialization (into JSON in this case) is out of the scope of the ORM, and also of the entire doctrine project at the moment.

@Ocramius Ocramius closed this Jan 6, 2016
@Ocramius Ocramius assigned Ocramius and unassigned beberlei Jan 6, 2016
@Ocramius Ocramius removed this from the 3.0 milestone Jan 6, 2016
@Ocramius Ocramius added the Won't Fix label Jan 6, 2016

I see your point, but ArrayCollection is not a part of ORM(?)... And as GabrielBull commented, that it belongs in a object that seeks to replace PHPs array?

Doctrine member

The doctrine collections library abstracts reading/writing of elements in a collection-alike structure: it doesn't abstract transforming the collection into something else (SRP still applies).

It is trivial to simply call toArray on a collection inside an entity's JsonSerializable implementation.


Yeah, thats true... thanks for the explanation :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment