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
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;
}