【问题标题】:How to create nested objects with FOSRestBundle and FormType?如何使用 FOSRestBundle 和 FormType 创建嵌套对象?
【发布时间】:2016-02-04 09:18:56
【问题描述】:

我正在使用 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 响应,错误为“实体传递到选择字段必须管理。也许将它们保存在实体管理器中?”

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

【问题讨论】:

    标签: json symfony fosrestbundle


    【解决方案1】:

    我找到了一种解决方案

    我已被创建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"}]}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-09
      • 2020-04-02
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      相关资源
      最近更新 更多