-3

My controller:

            /**
            * @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;

            }

The error message:

Class 'Productgroup' does not exist

peace_love
  • 6,229
  • 11
  • 69
  • 157

2 Answers2

1

I cant tell you why there's an class error, but you can't pass an entity object to a method that expects an ArrayCollection, here :

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

Maybe you should use addProductgroup().

If $argsId is an array of ids, you should get the reference of each one, and add the ghost objects to an ArrayCollection.

Zak
  • 1,005
  • 10
  • 22
0

working solution:

     /**
        * @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;

        }
peace_love
  • 6,229
  • 11
  • 69
  • 157