我怎么能存储与学说(Symfony的4)数组集合数据?

问题描述 投票:-2回答:2

我的控制器:

            /**
            * @Route("/row/{slug}/{connect}/{field}/{productgroup}", name="row", methods={"POST"})
            */

            public function row($slug, $connect, $field,$productgroup, Request $request) {

              $EntityName = 'App\\Entity\\' . ucwords($slug);
              $con = 'App\\Entity\\' . ucwords($connect);
              $entity = $this->getDoctrine()->getRepository($EntityName)->findOneBy(['id' => $field]);
              $entityManager = $this->getDoctrine()->getManager();
              $argsId = $productgroup;
              $args = $entityManager->getReference($connect , $argsId);
              $entity->setProductgroup($args);

              $entityManager->flush();
              $response = new Response();
              $response->send();
              return $response;

            }

错误信息:

类“Productgroup”不存在

symfony doctrine entity arraycollection
2个回答
1
投票

我不能告诉你为什么有一个类错误,但你不能传递一个实体对象给需要一个ArrayCollection,这里的方法:

/* args is not an ArrayCollection */
$args = $entityManager->getReference($connect , $argsId);
$entity->setProductgroup($args); 

也许你应该使用附加产品组()。

如果$ argsId是一组ID,你应该得到每个人的参考,并鬼对象添加到一个ArrayCollection。


1
投票

工作液:

     /**
        * @Route("/row/{entity}/{relation}/{entity_id}/{relation_id}", name="row", methods={"POST"})
        */

        public function row($entity, $relation, $entity_id, $relation_id, Request $request) {

          $entity_name = 'App\\Entity\\' . ucwords($entity);
          $relation_name = 'App\\Entity\\' . ucwords($relation);

          $entityManager = $this->getDoctrine()->getManager();
          $enity_reference = $entityManager->getReference($entity_name, $entity_id);
          $relation_reference = $entityManager->getReference($relation_name, $relation_id);

          $func = 'add'.$relation;
          $enity_reference->$func($relation_reference);
          $entityManager->persist($enity_reference);
          $entityManager->persist($relation_reference);

          $entityManager->flush();
          $response = new Response();
          $response->send();
          return $response;

        }
© www.soinside.com 2019 - 2024. All rights reserved.