如何使用学说(Symfony 4)将数据存储在数组集合中?

时间:2019-02-04 16:42:33

标签: symfony doctrine entity arraycollection

我的控制器:

PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"

错误消息:

  

“产品组”类别不存在

2 个答案:

答案 0 :(得分:1)

我不能告诉您为什么会出现类错误,但是您不能将实体对象传递给需要ArrayCollection的方法,在这里:

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

也许您应该使用addProductgroup()。

如果$ argsId是一个id数组,则应该获取每个id的引用,并将虚对象添加到ArrayCollection中。

答案 1 :(得分: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;

        }