我在尝试为ManyToMany关系生成表时出错。
在这两个实体中,我有ManyToMany关系,并且当我运行make:migration命令时,我得到错误'Column name FCO_ID referenced for relation from App\Entity\CINFO\CiRome App\Entity\CINFO\CiFormacode In not exist‘。
我不明白我做错了什么,这和教义文档中的完全一样。https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#many-to-many-unidirectional。
我的id的名字不是'id',但我在ManyToMany关系的JoinColumns中定义了它们。
/**
* @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;
}谢谢你的帮忙!
发布于 2020-07-04 21:40:28
按照@simon.ro的建议,我注意到我在JoinColumn中犯了一个错误。
这两个实体的更正如下:
/**
* @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;
}在CiFormacode中,我颠倒了id,您必须将JoinColumn的实际实体的id和要加入到inverseJoinColumns中的实体的id放在一起。实际上,这更有意义。
第二个实体修正:
/**
* @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;
}在CiRome中,您只需将变量'$FCO‘映射到'mappedBy’,即可获得对CiFormacode的引用。
不管怎么都要谢谢您。
https://stackoverflow.com/questions/62718299
复制相似问题