2017-04-11 1 views
1

내 웹 사이트에 api-platform (https://api-platform.com/) 프레임 워크를 사용하고 있으며 여러 조건으로 여러 번 컬렉션을 여러 번 직렬화하려고합니다.API-Platform 사용자 정의 필터링 된 컬렉션을 속성으로 필터링

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; 
    } 
} 

그리고 교리 포스트로드 이벤트에 대한 이벤트 가입자 : 나는 다음과 같은 예를 엔티티를 생성 한

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' 
     ]; 
    } 
} 

내가하려고 하나의 EntityWithFilteredCollection을 검색한다.

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

내가 잘못된 길을 가고 있는가 : 시간 나는 다음과 같은 예외가 true로 설정된 조건 값이 일부 FilteredCollectionRelatedEntity 엔티티를 가지고? 내가 놓친 게 있니? 필터링 된 컬렉션을 직렬화하려고하지는 않지만 원래 컬렉션은 작동하지만 모든 엔티티를 가져옵니다.

답변