API-Platform custom filtered collection as property

330
April 11, 2017, at 4:10 PM

I am using the api-platform (https://api-platform.com/) Framework for my website and I am trying to serialize a collection of an entity multiple times with different criteria. I have created the following example entities:

EntityWithFilteredCollection

namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
 * @ApiResource(attributes={"normalization_context"={"groups"={"get"}}})
 * @ORM\Entity()
 */
class EntityWithFilteredCollection
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     * @Groups({"get"})
     */
    private $id;
    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\FilteredCollectionRelatedEntity", mappedBy="relatedEntity")
     */
    private $relatedEntities;
    /**
     * @Groups({"get"})
     */
    private $filteredEntities;
    public function __construct()
    {
        $this->relatedEntities = new ArrayCollection();
    }
    public function getId()
    {
        return $this->id;
    }
    public function getRelatedEntities()
    {
        return $this->relatedEntities;
    }
    public function setRelatedEntities($relatedEntities)
    {
        $this->relatedEntities = $relatedEntities;
    }
    public function getFilteredEntities()
    {
        return $this->filteredEntities;
    }
    public function setFilteredEntities($filteredEntities)
    {
        $this->filteredEntities = $filteredEntities;
    }
}

FilteredCollectionRelatedEntity

namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
 * @ApiResource(attributes={"normalization_context"={"groups"={"get"}}})
 * @ORM\Entity()
 */
class FilteredCollectionRelatedEntity
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     * @Groups({"get"})
     */
    private $id;
    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\RelatedEntity", inversedBy="relatedEntites")
     */
    protected $relatedEntity;
    /**
     * @ORM\Column(type="integer")
     * @Groups({"get"})
     */
    protected $value;
    /**
     * @ORM\Column(type="boolean")
     * @Groups({"get"})
     */
    protected $condition;
    public function getId()
    {
        return $this->id;
    }
    public function getRelatedEntity()
    {
        return $this->relatedEntity;
    }
    public function setRelatedEntity($relatedEntity)
    {
        $this->relatedEntity = $relatedEntity;
    }
    public function getValue()
    {
        return $this->value;
    }
    public function setValue($value)
    {
        $this->value = $value;
    }
    public function getCondition()
    {
        return $this->condition;
    }
    public function setCondition($condition)
    {
        $this->condition = $condition;
    }
}

And an event subscriber for the Doctrine Post Load Event:

namespace AppBundle\EventSubscriber;

use AppBundle\Entity\EntityWithFilteredCollection;
use AppBundle\Entity\FilteredCollectionRelatedEntity;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
class FilterCollectionSubscriber implements EventSubscriber
{
    /** @var  RequestStack */
    private $requestStack;
    public function __construct(TokenStorage $tokenStorage, RequestStack $requestStack)
    {
        $this->tokenStorage = $tokenStorage;
        $this->requestStack = $requestStack;
    }

    public function postLoad(LifecycleEventArgs $event)
    {
        $entity = $event->getEntity();
        $method = $this->requestStack->getCurrentRequest()->getMethod();

        if (!$entity instanceof EntityWithFilteredCollection || Request::METHOD_GET !== $method) {
            return;
        }
        $entity->setFilteredEntities($entity->getRelatedEntities()->filter(function (FilteredCollectionRelatedEntity $entity) {
            return $entity->getCondition();
        }));
    }
    /**
     * Returns an array of events this subscriber wants to listen to.
     *
     * @return array
     */
    public function getSubscribedEvents()
    {
        return [
            'postLoad'
        ];
    }
}

When I try to retrieve one EntityWithFilteredCollection which has some FilteredCollectionRelatedEntity entities which have the condition value set to true I get the following exception:

No resource class found for object of type "AppBundle\Entity\FilteredCollectionRelatedEntity"

Am I going the wrong path? Am I missing something? When I don't try to serialize the filtered collection but the original one it works, but I get all entities.

Rent Charter Buses Company
READ ALSO
Checking if zip code exists in massive list of data

Checking if zip code exists in massive list of data

What would be the best solution for this problem?

385
regulate GROUP_CONCAT multible rows at table

regulate GROUP_CONCAT multible rows at table

iwant display like this :- //////////////////////////////////////

228
export nested PHP array to CSV

export nested PHP array to CSV

I would like to export this structure to CSV :

464
How to make SOAP call using SOAP WSDL in php

How to make SOAP call using SOAP WSDL in php

how to make SOAP call using below WSDL file

370