E
E
EVOSandru62021-11-04 16:22:54
Doctrine ORM
EVOSandru6, 2021-11-04 16:22:54

How to get link data correctly in Doctrine using 2 HasMany?

Good afternoon.

The user has several addresses, delivery is carried out to a specific address.
How is it possible to get through the user.deliveries link ?
Or is it only achieved through the repository?

class User
{
    #[ORM\OneToMany(mappedBy: "recipient", targetEntity: Address::class, cascade: ["persist"], orphanRemoval: true)]
    private Collection|array $addresses;
}

class Address
{
    #[Orm\ManyToOne(targetEntity: Recipient::class, inversedBy: 'addresses')]
    #[Orm\JoinColumn(name: "user_id", referencedColumnName: "id")]
    private User $user;
}

class Delivery {
    #[ORM\OneToOne(targetEntity: Address::class)]
    #[ORM\JoinColumn(name: "address_id", referencedColumnName: "id")]
    private Address $address,
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tukreb, 2021-11-04
@tukreb

So, according to the entities and the base, respectively, it turns out that only 1 package can be delivered to the address, and not many :)
In this case, you won’t be able to do it. Redesign the database or relationship between entities.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question