【问题标题】:JMS Serialized add condition to relation in entityJMS序列化向实体中的关系添加条件
【发布时间】:2018-02-02 12:41:05
【问题描述】:

我将 jms 序列化和 restbundle 用于 json 响应 我有实体关系

class Questions
{

    /**
 * @var ArrayCollection|Notes[]
 *
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Notes", mappedBy="questions", cascade={"persist"})
 * @Annotation\Groups({
 *     "get_question", "get_questions"
 * })
 */
private $note;

和我的行动

        $context = SerializationContext::create()->enableMaxDepthChecks();
    if ($groups) {
        $context->setGroups(["get_questions"]);
    }

    if ($withEmptyField) {
        $context->setSerializeNull(true);
    }

    return View::create()
        ->setStatusCode(self::HTTP_STATUS_CODE_OK)
        ->setData($questions)
        ->setSerializationContext($context);

一切正常,作为回应,我有 note 键和 Notes 对象

      "note": [
    {
      "author": {
          "id": 17,
          "name": "qq"
      },
      "id": 4,
      "text": "aaawww"
    },

但我想为此添加一些条件,例如我想要作者Notes,例如我想要过滤用户身份验证。我可以添加 SerializesAccessor 并添加“getter”,但我只能在实体中创建 getter 函数,如何在实体中获取身份验证用户...?或者如何解决这个问题?

【问题讨论】:

  • 您应该在注入安全性的服务中进行处理,如此处所述jmsyst.com/libs/serializer/master/event_system
  • 我创建了serializer.post_serialize,但是如何更改ObjectEvent $event 中的数据?
  • 我明白了,科尔

标签: symfony fosrestbundle jmsserializerbundle


【解决方案1】:

解决了。我使用 jms 序列化事件。在此事件之后,我可以通过身份验证用户条件在我的回复中更改 ArrayCollection

class SerializationListener implements EventSubscriberInterface
{
private $tokenStorage;

private $user;

private $notesRepository;

public function __construct(
    TokenStorageInterface $tokenStorage,
    NotesRepository $notesRepository
) {
    $this->tokenStorage = $tokenStorage;
    $this->notesRepository = $notesRepository;
    $this->user = $tokenStorage->getToken()->getUser();
}

/**
 * @inheritdoc
 */
static public function getSubscribedEvents()
{
    return array(
        array(
            'event' => 'serializer.pre_serialize',
            'class' => Questions::class,
            'method' => 'onPreSerialize'
        )
    );
}

public function onPreSerialize(PreSerializeEvent $event)
{
    /** @var Questions $question */
    $question = $event->getObject();
    if ($this->user instanceof User) {
        $authorNotes = $this->notesRepository->findBy(['user' => $this->user, 'questions' => $question]);
        $question->setNoteCollection($authorNotes);
    }
}
}

和配置

    app.listener.serializationlistener:
    class: "%app.serialization_listener.class%"
    arguments:
        - "@security.token_storage"
        - "@app.repository.notes"
    tags:
        - {name: jms_serializer.event_subscriber}

并设置集合

    /**
 * @param Notes[] $notes
 * @return $this
 */
public function setNoteCollection(array $notes)
{
    $this->getNote()->clear();
    foreach ($notes as $note) {
        $this->addNote($note);
    }

    return $this;
}

【讨论】:

    猜你喜欢
    • 2013-04-06
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多