Answer the question
In order to leave comments, you need to log in
How to specify a link to another entity in symfony 4 when linking in anatomy?
Guys tell me, I'm completely confused.
Here I have an entity:
class A{
/**
* @ORM\Column(name="system_key", type="string", length=20)
*/
private $system_key;
}
class B{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\A", mappedBy="system_key")
*/
private $exchange;
}
does not have a property named "mappedBy". Available properties:
targetEntity, cascade, fetch, inversedBy
Answer the question
In order to leave comments, you need to log in
And so
/**
* @ORM\ManyToOne(targetEntity="App\Entity\A", inversedBy="system_key")
* @ORM\JoinColumn()
*/
It is possible to connect entities with each other only with the participation of at least one primary key of these entities, because Doctrine distinguishes between entities due to the primary key, just like in database theory in general. Thus, in this case, the system_key field does not characterize entity A as a whole, but is simply one of its aspects along with other fields. That is, we need a criterion by which one can unambiguously distinguish an entity of class A from another entity of the same class. Such a criterion, as already understood, is the id field, i.e. primary key that distinguishes one record from another
The properties mappedBy, inversedBy in the ManyToOne, OneToMany annotations should be specified only if a bidirectional relationship between entities is needed, i.e. when B is available from A and vice versa. If unidirectional communication is required, i.e. it is enough to get A from B, then Doctrine must explicitly specify the associated fields through JoinColumn. In this case, it is enough to manipulate class B, for example:
class B
{
/**
* @ORM\ManyToOne(targetEntity="A")
* @ORM\JoinColumn(name="a_id", referencedColumnName="id")
*/
private $address;
}
class A
{
// ...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question