如何使用 FOSRestBundle 和 FormType 创建嵌套对象?

How to create nested objects with FOSRestBundle and FormType?

我正在使用 symfony2 + FOSRestBundle 开发一个 API,我有两个错误。 下面是我的代码:

属性

/**
 * Property
 *
 * @ORM\Table(name="property")
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"house" = "House"})
 */
abstract class Property {

    /**
     * @ORM\OneToMany(targetEntity="Image", mappedBy="property", cascade={"persist"})
     * */
    private $images;


    function getImages() {
        return $this->images;
    }

    function setImages($images) {
        $this->images = $images;
    }


}

房子

class House extends Property
{
/* More code */
}

图片

class Image {

    /**
     * @ORM\Column(name="content", type="text", nullable=false)
     */
    private $content;

    /**
     * @ORM\ManyToOne(targetEntity="Property", inversedBy="images")
     * @ORM\JoinColumn(name="propertyId", referencedColumnName="id")
     * */
    private $property;
}

属性类型

class PropertyType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('images');


        $builder->get('images')
                ->addModelTransformer(new CallbackTransformer(
                        function($images) {
                            $image = new \Cboujon\PropertyBundle\Entity\Image();
                            $image->setContent('test of content');
                            return array($image);
                }, function($imagesContents) {

                }));
    }

HouseRESTController

/**
 * @View(statusCode=201, serializerEnableMaxDepthChecks=true)
 *
 * @param Request $request
 *
 * @return Response
 *
 */
public function postAction(Request $request)
{ 
    $entity = new House(); 
    $form = $this->createForm(new HouseType(), $entity, array("method" => $request->getMethod()));
    $this->removeExtraFields($request, $form);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $entity;
    }

当我创建新房子时,我发送这个(简体)JSON:

{"images":["base64ContentImage_1", "base64ContentImage_2"]}

第一个问题: 传递给 CallbackTransformer 的第一个函数中的 $images 参数为 NULL。为什么?

第二个问题:我为了测试和理解第一个问题,我强制创建了一个图像实体,如您所见,但我得到了 JSON 响应错误 "Entities passed to the choice field must be managed. Maybe persist them in the entity manager?"

谁能帮我解决两个问题中的任何一个?

我找到了一个解决方案

我被创造了ImageType

public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);
    $builder
        ->add('content')
    ;
}

而且我也被修改了PropertyType

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('title')
            ->add('description')
            ->add('price')
            ->add('services')
            ->add('images', 'collection', array(
                'type' => new ImageType(),
                'allow_add' => true,
            ))
    ;
}

最后,我更改了请求的 JSON 结构:

{"images":[{content: "base64ContentImage_1"}, {content:"base64ContentImage_2"}]}