Php 控制器没有';t返回Symfony 2中的一个或多个关系字段

Php 控制器没有';t返回Symfony 2中的一个或多个关系字段,php,symfony,doctrine,Php,Symfony,Doctrine,我需要返回带有文档模型的完整响应。我有回应,但缺少一些字段,它们是在实体中定义的。例如,我需要有“活动”和“模板”属性作为响应,但实际上“活动”是不存在的 下面是我的控制器和实体 我在我的控制器中有这样的操作: /** * @REST\View(serializerGroups={"Default", "DocumentDetails"}) * @REST\Get("/{id}", requirements={"id" = "\d+"}) * @ParamConverter("docume

我需要返回带有
文档
模型的完整响应。我有回应,但缺少一些字段,它们是在实体中定义的。例如,我需要有“活动”和“模板”属性作为响应,但实际上“活动”是不存在的

下面是我的控制器和实体

我在我的控制器中有这样的操作:

/**
 * @REST\View(serializerGroups={"Default", "DocumentDetails"})
 * @REST\Get("/{id}", requirements={"id" = "\d+"})
 * @ParamConverter("document", class="AppBundle:Document");
 */
public function showAction(Request $request, Document $document)
{
    return $document;
}
但文档实体具有以下关系:

/**
* Document entity
 *
 * @ORM\Entity(repositoryClass="AppBundle\Repository\DocumentRepository")
 * @ORM\Table(name="document")
 * @ORM\HasLifecycleCallbacks()
 *
 * @Serializer\ExclusionPolicy("all")
 */
class Document
{
.......

/**
 * @var campaign
 * @ORM\ManyToOne(targetEntity="Campaign", inversedBy="documents")
 * @ORM\JoinColumn(name="campaign", referencedColumnName="id")
 *
 * @Serializer\Expose()
 */
protected $campaign; // **THIS FIELD IS ABSENT - WHY !???** 

/**
 * @var DocumentTemplate Szablon dokumentu
 *
 * @ORM\ManyToOne(targetEntity="DocumentTemplate")
 * @ORM\JoinColumn(name="template_id", referencedColumnName="id")
 *
 * @Serializer\Expose()
 */
protected $template; // **THIS PROPERTY IS DISPLAYED**

.......

$document->template
出现在$document响应中。但是
$document->campaign
不存在。怎么了?它可能与
序列化组
有某种关系??谢谢你的帮助。

解决了!谢谢大家的帮助。该问题与JMSSerializer有关。 首先需要在配置文件
services.yml
中设置此序列化程序:

app.serializer.listener.document:
    class: AppBundle\EventListener\Serializer\DocumentSerializationListener
    tags:
        - { name: jms_serializer.event_subscriber }
然后创建此侦听器,该侦听器正在创建表单子字段
活动
,并在其中插入活动对象:

<?php


namespace AppBundle\EventListener\Serializer;


use AppBundle\Entity\Campaign;
use AppBundle\Entity\Document;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;

class DocumentSerializationListener implements EventSubscriberInterface
{
    /**
     * @param ObjectEvent $event
     * @return void
     */
    public function onPostSerialize(ObjectEvent $event)
    {
        $entity = $event->getObject();

        if (!($entity instanceof Document)) {
            return ;
        }

        $groups = $event->getContext()->attributes->get('groups')->getOrElse([]);

        if (in_array('DocumentDetails', $groups)) {
            $visitor = $event->getVisitor();

            $campaign = $this->getCampaignClone($entity->getCampaign());

            if ($visitor->hasData('campaign')) {
                $visitor->setData('campaign', $campaign);
            } else {
                $visitor->addData('campaign', $campaign);
            }
        }
    }

    /**
     * @inheritdoc
     */
    public static function getSubscribedEvents()
    {
        return [
            [
                'event' => 'serializer.post_serialize',
                'class' => 'AppBundle\Entity\Document',
                'method' => 'onPostSerialize'
            ]
        ];
    }

    private function getCampaignClone(Campaign $documentCampaign)
    {
        $campaign = new \stdClass();
        $campaign->id = $documentCampaign->getId();
        $campaign->title = $documentCampaign->getTitle();
        $campaign->status = $documentCampaign->getStatus();
        $campaign->rows = $documentCampaign->getRows();
        $campaign->createdAt = $documentCampaign->getCreatedAt()->format(DATE_W3C);
        $campaign->updatedAt = $documentCampaign->getUpdated()->format(DATE_W3C);

        return $campaign;
    }
}

您确定
活动
不为空吗?是的,在db中它被设置为与现有活动相关的有效id号。您是否有getter/setter?如果您获取的是模板而不是活动,则代码中的某个地方有错误。请尝试运行“\doctor\Common\Util\Debug::dump($document)”,并查看输出