F
F
ff0xff2019-01-15 18:07:43
symfony
ff0xff, 2019-01-15 18:07:43

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;

}

I need that in entity B the value of the field of entity A is set as the field value
class B{
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\A", mappedBy="system_key")
     */
    private $exchange;
}

if i put mappedBy it gives an error
does not have a property named "mappedBy". Available properties:
targetEntity, cascade, fetch, inversedBy

how to connect them tell me?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2019-01-15
@sidni

And so

/**
     * @ORM\ManyToOne(targetEntity="App\Entity\A", inversedBy="system_key")
     * @ORM\JoinColumn()
     */

D
dosim86, 2019-01-24
@dosim86

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
{
    // ...
}

where, name="a_id" is a field of table B, referencedColumnName="id" is the primary key of table A

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question