我试图弄清楚在使用FOSRestBundle和JMSSerializer时如何在视图中返回DBRef对象。
所以在我的文件中我有
class Advert {
//...
/**
* @var Image[]
* @MongoDB\ReferenceMany(targetDocument="AppBundle\Document\Image", cascade={"persist", "update", "remove"})
* @Serializer\Groups({"Default", "list", "details"})
* @Serializer\AccessType("public_method")
* @Serializer\Accessor(getter="getSerializableImages")
*/
protected $images;
//...
}
然后,我的控制器看起来像这样
/**
* @View(serializerGroups={"details"})
* @ParamConverter("advert", class="AppBundle:Advert")
* @param Request $request
* @param Advert $advert
* @return array
* @throws \Doctrine\ODM\MongoDB\LockException
*/
public function getAction(Request $request, Advert $advert)
{
$this->get('monolog.logger.advert')->info('Advert View', [
'documents' => [
'advert' => $advert->toLoggableArray(),
],
]);
$advertContact = new AdvertContact();
$advertContact->setAdvert($advert);
$form = $this->createForm('AppBundle\Form\AdvertContact\AdvertContactType', $advertContact, [
'action' => $this->generateUrl('advert_contact_new'),
'method' => 'post',
]);
return [
'advert' => $advert,
'contact_form' => $form->createView(),
];
}
我一直在检查解决方案。我最近得到的是使用JMS的@Accessor
注释,使用了对文档执行$this->images->toArray()
的方法,但这种方法无法正常工作,我担心反序列化。
非常感谢任何帮助!