A
A
Alexander Zemlyanoy2020-12-23 20:58:33
symfony
Alexander Zemlyanoy, 2020-12-23 20:58:33

How do I create a ManyToOne relationship when joining 2 columns?

Good day to all!

I have 2 tables, one with prices, the other with stock balances, I need to register the relationship between them. In this case, the join will be in 2 fields (warehouse and product id). More details on the screenshot
How can I do it right?
The option below does not work.

class Price {
    /**
     * @ORM\ManyToOne(targetEntity="\CRMStoreBundle\Entity\Inventory", inversedBy="prices", cascade={"persist"})
     * @ORM\JoinColumns({
     *      @ORM\JoinColumn(name="store_id", referencedColumnName="store_id"),
     *      @ORM\JoinColumn(name="offer_id", referencedColumnName="offer_id")
     * })
     */
    private $inventory;
}

class Inventory {
    /**
     * @var \CoreBundle\Entity\PurchasePrice
     *
     * @ORM\OneToMany(targetEntity="\CoreBundle\Entity\PurchasePrice", mappedBy="inventory", fetch="EAGER")
     * @Serializer\Exclude
     */
    protected $prices;
}

5fe3842283a53227112493.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tukreb, 2020-12-24
@Galamoon

It is impossible in the Doctrine to join one entity by 2 fields at once, if it is not a ManyToMany (which, in turn, cannot contain anything but 2 field IDs, otherwise you will have to manually break the connection into ManyToOne and select separate IDs).
You already have a special ID for them and describe the ManyToOne relationship
For example

class Inventory {
    /**
     * @ORM\ManyToOne(targetEntity=Price::class, inversedBy="prices")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_price", referencedColumnName="id")
     * })
     */
    protected $prices;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question