Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question