使用FosrestBundle在Symfony 4中的1个帖子(REST API)中保留多个实体

时间:2018-04-23 15:44:13

标签: rest api symfony fosrestbundle

我想使用fosrestbundle在1个帖子中发布几个实体。目前,持续1个实体运作良好,但我不确定如何继续进行多个实体。

我现在尝试的是创建一个只包含1个属性的 MyEntityCollection类,它是MyEntity的集合:

class MyEntityCollection
{
/*
 * @var Collection
 */
protected $myEntities;

/**
 * @return mixed
 */
public function getMyEntities()
{
    return $this->myEntities;
}

/**
 * @param mixed $myEntities
 */
public function setMyEntities($myEntities): void
{
    $this->myEntities= $myEntities;
}

/**
 * @param MyEntity $myEntity
 * @return MyEntityCollection
 */
public function addMyEntity(MyEntity $myEntity): MyEntityCollection
{
    $this->myEntities->add($myEntity);

    return $this;
}

}

我还创建了 MyEntityCollectionType formType

class MyEntityCollectionType extends AbstractType
{
/**
 * This function sets the fields of the form
 * @param FormEvent $event the PRE_SET_DATA event
 */
public function setFields(FormEvent $event): void
{
    $form = $event->getForm();

    $form
        ->add('myEntities', EntityType::class, array(
            'required'   => true,
            'multiple' => true,
            'class' => MyEntity::class
        ));
        //->add('myEntities', CollectionType::class, array(
        //    'required'   => true,
        //    'data_class' => MyEntity::class
        //));

}

我的控制器操作如下所示:

protected function processForm($entity, String $entityTypeClass, $request)
{
    if (empty($entity)) {
        throw new NotFoundHttpException('Entity not found');
    }

    $form = $this->createForm(
        $entityTypeClass,
        $entity
    );
    $form->submit($request->request->all(), false);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $entity;
    }

    return $form;
   }
   /**
    * @Rest\View(statusCode=Response::HTTP_CREATED)
    * @Rest\Post("/myEntity-collection")
    * @param Request $request
    * @return object
    */
    public function postMyEntityCollectionAction(Request $request): object
    {
        $myEntityCollection = new MyEntityCollection();

        return $this->processForm($myEntityCollection, MyEntityCollectionType::class, $request);
    }

当我尝试将数据发布到路由时,我收到此错误: Class" App \ Enablon \ Checklist \ Desktop \ Domain \ MyEntityCollection"不是有效的实体或映射的超类。

为了清楚起见,我不想坚持MyEntityCollection实体,只有几个MyEntity,但在我看来,如果它需要暴露在后期路由中,它就像创建实体类一样干净的方式

也许我没有采取正确的方法,所以我对任何建议都持开放态度,但在我看来,这似乎是一种干净的方式来实现我想做的事情。

1 个答案:

答案 0 :(得分:0)

我不确定这是否正确,但这是一个工作案例:

我删除了MyEntityCollection实体及其表单类型。相反,我在我的控制器中执行此操作:

/**
 * @Rest\View(statusCode=Response::HTTP_CREATED)
 * @Rest\Post("/myentity-collection")
 * @param Request $request
 * @return object
 */
public function postMyEntityCollectionAction(Request $request)
{
    $validation = array();
    foreach ($request->request->all() as $myEntityRequest) {
        $validation[] = $this->processForm(new MyEntity(), MyEntityType::class, $myEntityRequest);
    }

    return $validation;
}

我只是在MyEntity上发布了一个数组,作为我路线的一个参数。

这样验证错误更容易理解,因为它们都在控制器返回的$ validation数组中。