doctrine / DoctrineModule

Doctrine Module for Laminas
http://www.doctrine-project.org/projects/doctrine-module.html
MIT License
398 stars 270 forks source link

Mapping error doctrine find, findOneBy, findBy. #819

Closed vitor5g closed 11 months ago

vitor5g commented 1 year ago

Hello, good morning, I have a problem in my project that I believe is related to some type of DOCTRINE cache, but I can't solve it. In my project in some places when I use the find or findBy or findOneBy methods, to bring data from my database, when using object mapping through the getter methods provided by doctrine when using, for example, the getCodUsuario()- command >getCodUsuario() in my class the user_code is returned, different from the one actually present in the main entity. Maybe someone can help me? Below are some principles of how my entities are, both the main one TbUsuario and one that has the user_code as a foreign key. TbAemNcbr.txt TbPessoaFisicaComplementar.txt TbUsuario.txt TbPessoa.txt

image

image

image

image

image

Here is this part of my code, notice that it shows the codusuario = 1 on the screen and not what is actually in the main entity. Could anyone help me with this error please? Thanks

TomHAnderson commented 11 months ago

Though I don't think I got to the root of your problem, perhaps this will help:

The primary key for TbUsuario is

    /**
     * @var \SRH\Entity\TbPessoaFisicaComplementar
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\OneToOne(targetEntity="SRH\Entity\TbPessoaFisicaComplementar")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="cod_usuario", referencedColumnName="cod_pessoa")
     * })
     */
    private $codUsuario;

You should not have a 1:1 relationship on a primary key. Instead, a foreign key on one of the two entities should store the primary key of the other. As this is not a generated value I assume you're sharing primary keys. This is not good practice.

Much of your ERD is unusual. Primary keys are traditionally named id but doing it differently is OK. However, your TbPessoaFisicaComplementar entity shares a primary key name with TbPessoa.

Because all your foreign keys are not required, I feel you would benefit from a tool to help you create your metadata. https://skipper18.com is such a tool.

This question is not specific to this module and though this reply is slow, I hope it helps you along.