Skip to content

ArrayCollection:RemoveElement dont work #2

@francyfox

Description

@francyfox

I cant remove the group. Cascade remove and orphan doesn't work. Called ArrayCollection:RemoveElement

  • Entity User.class
 /**
     * @Groups({"read", "write"})
     * Many Users have Many Groups.
     * @ORM\ManyToMany(targetEntity="Group", cascade={"persist", "remove"}, orphanRemoval=true)
     * @ORM\JoinTable(name="users_groups",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     *      )
     */
    private $groups;
 public function removeGroup(\App\Entity\Group $group){
        if (!$this->groups->contains($group)) {
            return;
        }
        $this->groups->removeElement($group);
    }

Controller


        $user = $this->getDoctrine()
            ->getRepository(User::class)
            ->find($id);

        $group = $this->getDoctrine()
            ->getRepository(Group::class)
            ->find($groupId);
        $user->removeGroup($group);
        $this->em->flush();
        return new Response('', 204);

ERROR

class: "Error"
detail: "Call to a member function flush() on null"
status: 500
title: "An error occurred"
trace: [{namespace: "", short_class: "", class: "", type: "", function: "",…},…]
0: {namespace: "", short_class: "", class: "", type: "", function: "",…}
1: {namespace: "App\Controller", short_class: "UserJoin", class: "App\Controller\UserJoin", type: "->",…}
2: {namespace: "Symfony\Component\HttpKernel", short_class: "HttpKernel",…}
3: {namespace: "Symfony\Component\HttpKernel", short_class: "HttpKernel",…}
4: {namespace: "Symfony\Component\HttpKernel", short_class: "Kernel",…}
type: "https://tools.ietf.org/html/rfc2616#section-10"

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions