0
votes

I got an error when I'm trying generate table for ManyToMany relation.

In these two entities I have the ManyToMany relationship, and when I'm running make:migration command I got error 'Column name FCO_ID referenced for relation from App\Entity\CINFO\CiRome towards App\Entity\CINFO\CiFormacode does not exist'.

I don't understand what I'm doing wrong, it's exactly like in doctrine documentation. https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#many-to-many-unidirectional.

My id's names are not 'id', but I defined them in the JoinColumns of ManyToMany relationship.

/**
 * @ORM\Entity(repositoryClass=CiFormacodeRepository::class)
 * @ORM\Table(name="ci_formacode", indexes={
 *  @Index(name="FCO_CODE", columns={"FCO_CODE"})
 * })
 */
class CiFormacode
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer", name="FCO_ID")
     */
    private $FCO_ID;

    /**
     * @ORM\ManyToOne(targetEntity=CiFormacode::class, inversedBy="ciFormacodes")
     * @JoinColumn(name="FCO_PERE_ID", referencedColumnName="FCO_ID", nullable=true)
     */
    private $FCO_PERE;

    /**
     * @ORM\ManyToMany(targetEntity=CiRome::class, mappedBy="FCO")
     * @ORM\JoinTable(name="ci_fcorome", joinColumns={@JoinColumn(name="ROME_ID", referencedColumnName="rome_id")})
     */
    private $ciRomes;
}


/**
 * @ORM\Entity(repositoryClass=CiRomeRepository::class)
 * @ORM\Table(name="ci_rome")
 */
class CiRome
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer", name="rome_id")
     */
    private $rome_id;

    /**
     * @ORM\ManyToOne(targetEntity=CiRomeDp::class, inversedBy="ciRomes")
     * @JoinColumn(name="romedp_id", referencedColumnName="romedp_id", nullable=true)
     */
    private $romedp;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $rome_code;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $rome_lib;

    /**
     * @ORM\OneToMany(targetEntity=CiRomeAppelation::class, mappedBy="ROME")
     */
    private $ciRomeAppelations;


    /**
     *
     * @ManyToMany(targetEntity="App\Entity\CINFO\CiFormacode")
     * @JoinTable(name="ci_fcorome",
     *      joinColumns={@JoinColumn(name="FCO_ID", referencedColumnName="FCO_ID")},
     *      inverseJoinColumns={@JoinColumn(name="rome_id", referencedColumnName="rome_id")}
     *      )
     */
    private $FCO;
}

Thanks for your help!

1
Please put the relevant code directly into your question.Vyctorya
Yes sorry, it's done.Vlad Vorobyev
you should re-check the @JoinColumnsimon.ro

1 Answers

0
votes

Following @simon.ro advice I noticed that I made a mistake in JoinColumn.

Correction of these two entities bellow:

/**
 * @ORM\Entity(repositoryClass=CiFormacodeRepository::class)
 * @ORM\Table(name="ci_formacode", indexes={
 *  @Index(name="FCO_CODE", columns={"FCO_CODE"})
 * })
 */
class CiFormacode
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer", name="FCO_ID")
     */
    private $FCO_ID;

    /**
     * @ORM\ManyToOne(targetEntity=CiFormacode::class, inversedBy="ciFormacodes")
     * @JoinColumn(name="FCO_PERE_ID", referencedColumnName="FCO_ID", nullable=true)
     */
    private $FCO_PERE;

    /**
     * @ManyToMany(targetEntity="App\Entity\CINFO\CiRome")
     * @JoinTable(name="ci_fcorome",
     *      joinColumns={@JoinColumn(name="FCO_ID", referencedColumnName="FCO_ID")},
     *      inverseJoinColumns={@JoinColumn(name="ROME_ID", referencedColumnName="rome_id")}
     *      )
     */
    private $ciRomes;
}

In CiFormacode I reversed id, you have to put the id of the actual entity for the JoinColumn, and the id of the entity that you would join in inverseJoinColumns. Effectively this make more sense.

Second entity correction:

/**
 * @ORM\Entity(repositoryClass=CiRomeRepository::class)
 * @ORM\Table(name="ci_rome")
 */
class CiRome
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer", name="rome_id")
     */
    private $rome_id;

    /**
     * @ORM\ManyToOne(targetEntity=CiRomeDp::class, inversedBy="ciRomes")
     * @JoinColumn(name="romedp_id", referencedColumnName="romedp_id", nullable=true)
     */
    private $romedp;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $rome_code;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $rome_lib;

    /**
     * @ORM\OneToMany(targetEntity=CiRomeAppelation::class, mappedBy="ROME")
     */
    private $ciRomeAppelations;


    /**
     * @ManyToMany(targetEntity="App\Entity\CINFO\CiFormacode", mappedBy="ciRomes")
     */
    private $FCO;
}

And in CiRome you just have to map variable '$FCO' with 'mappedBy' to get the reference to CiFormacode.

Thank you anyway.