如何删除实体关系(Symfony 4)?

How can I remove entity relations (Symfony 4)?

我的 table 字段:

id  name
1   cat
2   dog 
3   frog

我的table产品组

id  name
1   animals
2   food 
3   colors

我的tablefields_productgroup

fields_1  productgroup_1
1         1
2         1
3         2

我现在要做的是去掉frogcolors的关系。

我的控制器:

public function remove($entity, $id, $relation, $relation_id, Request $request)
{
    $obj = $this->getDoctrine()->getRepository(fields::class)->findOneBy(['id' => $id]);
    $entity_id = $obj->getId();

    $enity_reference = $entityManager->getReference(fields::class, $entity_id);
    $relation_reference = $entityManager->getReference(productgroup::class, $relation_id);

    $func = 'removeProductgroup';
    $enity_reference->$func($relation_reference);

    $entityManager->persist($enity_reference);
    $entityManager->persist($relation_reference);
    $entityManager->flush();
    $response = new Response();
    $response->send();
    return $response;
}

这是我的字段实体中的函数:

public function removeProductgroup(Productgroup $productgroup)
{
    $this->productgroup->removeElement($productgroup)

    return $this;
}

但我收到错误消息:

syntax error, unexpected 'return' (T_RETURN)

如果您答案中的代码与您使用的代码完全相同,那么您在以下代码段中存在语法错误:

public function removeProductgroup(Productgroup $productgroup)
{
    $this->productgroup->removeElement($productgroup) <i>// missing semicolon here</i>

    return $this;
}

显然应该是:

public function removeProductgroup(Productgroup $productgroup)
{
    $this->productgroup->removeElement($productgroup);

    return $this;
}

您必须用分号结束语句行。解释器看到关键字 return 并抛出语法错误。对于 PHP 白色 space 没有区别,因此它将代码解释为:

$this->productgroup->removeElement($productgroup) return $this;