【问题标题】:How can I remove entity relations (Symfony 4)? [closed]如何删除实体关系(Symfony 4)? [关闭]
【发布时间】:2019-02-08 18:01:48
【问题描述】:

我的表格字段:

id  name
1   cat
2   dog 
3   frog

我的餐桌产品组

id  name
1   animals
2   food 
3   colors

我的表字段_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;
}

但我收到错误消息:

语法错误,意外'return' (T_RETURN)

【问题讨论】:

    标签: php symfony doctrine entity symfony4


    【解决方案1】:

    如果您的答案中的代码正是您使用的代码,那么您在以下 sn-p 中存在语法错误:

    public function removeProductgroup(Productgroup $productgroup)
    {
        $this->productgroup->removeElement($productgroup) // missing semicolon here
    
        return $this;
    }

    显然应该是:

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

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

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

    【讨论】:

    • 没想到,竟然是这么简单的错误……
    • 我没有否决您的问题。只有投反对票的人才知道真正的原因。我想这是因为语法错误,假设通过彻底的代码自省很容易找到。我很高兴它现在可以工作了。
    猜你喜欢
    • 2017-02-27
    • 1970-01-01
    • 2022-07-30
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多