“Smart” Accessor in Doctrine Entity

Easily add some logic to filter out specific entities from an ArrayCollection in Symfony2. In this simple example, I wrote an app that had Node, and Comment entities separately, as well as an explicitly defined NodeComment entity. In my Node entity, I want to serialize all of the associated comments. But rather than call ->getNodeComments() and then filter in AngularJS (or Twig, or whatever else for that matter), I just want to know that my object node.comments are the actual comments I care about.

The Entity property

The Accessor

Now, my serialized object.comments are the actual comments, instead of the ManyToMany Entity.

One Reply to ““Smart” Accessor in Doctrine Entity”

  1. Why don’t you use array_map (or ArrayCollection::map())?

    return $this->node_comments->map(function ($nodeComment) {
    return $nodeComment->getComment();

Leave a Reply

Your email address will not be published. Required fields are marked *