How to fetch data from many to many relation ship third table in symfony doctrine

I have an intermediate table that acts as a pivot between two others: user, punto_suministro and table pivot user_punto_suministro:

enter image description here

How to fetch data from table user to table punto_suministro?

Entity User:

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 */
class User implements UserInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    .....
    

    /**
     * @ORM\OneToMany(targetEntity=UserPuntoSuministro::class, mappedBy="user")
     */
    private $userPuntoSuministros;

    /**
     * @throws \Exception
     */
    public function __construct()
    {
        $this->puntoSuministros = new ArrayCollection();
        $this->userPuntoSuministros = new ArrayCollection();
    }



    /**
     * @return Collection|UserPuntoSuministro[]
     */
    public function getUserPuntoSuministros(): Collection
    {
        return $this->userPuntoSuministros;
    }

    public function addUserPuntoSuministro(UserPuntoSuministro $userPuntoSuministro): self
    {
        if (!$this->userPuntoSuministros->contains($userPuntoSuministro)) {
            $this->userPuntoSuministros[] = $userPuntoSuministro;
            $userPuntoSuministro->setUser($this);
        }

        return $this;
    }

    public function removeUserPuntoSuministro(UserPuntoSuministro $userPuntoSuministro): self
    {
        if ($this->userPuntoSuministros->contains($userPuntoSuministro)) {
            $this->userPuntoSuministros->removeElement($userPuntoSuministro);
            // set the owning side to null (unless already changed)
            if ($userPuntoSuministro->getUser() === $this) {
                $userPuntoSuministro->setUser(null);
            }
        }

        return $this;
    }

Entity PuntoSuministro:

/**
 * @ORM\Entity(repositoryClass=PuntoSuministroRepository::class)
 */
class PuntoSuministro
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    ...

    /**
     * @ORM\OneToMany(targetEntity=UserPuntoSuministro::class, mappedBy="punto_suministro")
     */
    private $userPuntoSuministros;

    public function __construct()
    {
        $this->userPuntoSuministros = new ArrayCollection();
    }


    /**
     * @return Collection|UserPuntoSuministro[]
     */
    public function getUserPuntoSuministros(): Collection
    {
        return $this->userPuntoSuministros;
    }

    public function addUserPuntoSuministro(UserPuntoSuministro $userPuntoSuministro): self
    {
        if (!$this->userPuntoSuministros->contains($userPuntoSuministro)) {
            $this->userPuntoSuministros[] = $userPuntoSuministro;
            $userPuntoSuministro->setPuntoSuministro($this);
        }

        return $this;
    }

    public function removeUserPuntoSuministro(UserPuntoSuministro $userPuntoSuministro): self
    {
        if ($this->userPuntoSuministros->contains($userPuntoSuministro)) {
            $this->userPuntoSuministros->removeElement($userPuntoSuministro);
            // set the owning side to null (unless already changed)
            if ($userPuntoSuministro->getPuntoSuministro() === $this) {
                $userPuntoSuministro->setPuntoSuministro(null);
            }
        }

        return $this;
    }

Entity UserPuntoSuministro (table pivot):

/**
* @ORM\Entity(repositoryClass=UserPuntoSuministroRepository::class)
*/
class UserPuntoSuministro
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity=User::class, inversedBy="userPuntoSuministros")
 */
private $user;

/**
 * @ORM\ManyToOne(targetEntity=PuntoSuministro::class, inversedBy="userPuntoSuministros")
 */
private $punto_suministro;


public function getId(): ?int
{
    return $this->id;
}

public function getUser(): ?User
{
    return $this->user;
}

public function setUser(?User $user): self
{
    $this->user = $user;

    return $this;
}

public function getPuntoSuministro(): ?PuntoSuministro
{
    return $this->punto_suministro;
}

public function setPuntoSuministro(?PuntoSuministro $punto_suministro): self
{
    $this->punto_suministro = $punto_suministro;

    return $this;
}

I try and access as follows:

    $user = $userRepository->find($this->security->getUser());
    $pivote = $user->getUserPuntoSuministros();

but from here I don't know how to recover the punto_suministro of a user to show it on twig.

Thanks in advance.



Read more here: https://stackoverflow.com/questions/64897816/how-to-fetch-data-from-many-to-many-relation-ship-third-table-in-symfony-doctrin

Content Attribution

This content was originally published by In0cybe at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: