[英]Symfony 5 - OneToMany does not appear in my mysql database
So I have two tables: one is "cable" -> the main one AND the other one is "color"所以我有两张桌子:一张是“电缆”-> 主要一张,另一张是“颜色”
So i created an entity inside cable called colors linked to the color table with the OneToMany relation.因此,我在电缆内创建了一个名为 colors 的实体,该实体通过 OneToMany 关系链接到颜色表。 But the problem is that the migration:migrate does not appear in my database when the other ManyToOne relation does.但问题是,当其他 ManyToOne 关系出现时,migration:migrate 不会出现在我的数据库中。
Here it what's inside my migration.php file:这是我的 migration.php 文件中的内容:
` `
final class Version20210411225338 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE cables DROP FOREIGN KEY FK_9A9430025ED47289');
$this->addSql('DROP INDEX IDX_9A9430025ED47289 ON cables');
$this->addSql('ALTER TABLE cables DROP couleurs_id');
$this->addSql('ALTER TABLE colors ADD cables_id INT NOT NULL');
$this->addSql('ALTER TABLE colors ADD CONSTRAINT FK_C2BEC39F4DE1706A FOREIGN KEY (cables_id) REFERENCES cables (id)');
$this->addSql('CREATE INDEX IDX_C2BEC39F4DE1706A ON colors (cables_id)');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE cables ADD couleurs_id INT NOT NULL');
$this->addSql('ALTER TABLE cables ADD CONSTRAINT FK_9A9430025ED47289 FOREIGN KEY (couleurs_id) REFERENCES colors (id)');
$this->addSql('CREATE INDEX IDX_9A9430025ED47289 ON cables (couleurs_id)');
$this->addSql('ALTER TABLE colors DROP FOREIGN KEY FK_C2BEC39F4DE1706A');
$this->addSql('DROP INDEX IDX_C2BEC39F4DE1706A ON colors');
$this->addSql('ALTER TABLE colors DROP cables_id');
}
}
` My Cable.php file: ` 我的 Cable.php 文件:
` `
/**
* @return Collection|Colors[]
*/
public function getCouleurs(): Collection
{
return $this->couleurs;
}
public function addCouleur(Colors $couleur): self
{
if (!$this->couleurs->contains($couleur)) {
$this->couleurs[] = $couleur;
$couleur->setCables($this);
}
return $this;
}
public function removeCouleur(Colors $couleur): self
{
if ($this->couleurs->removeElement($couleur)) {
// set the owning side to null (unless already changed)
if ($couleur->getCables() === $this) {
$couleur->setCables(null);
}
}
return $this;
}
/**
* @ORM\OneToMany(targetEntity=Colors::class, mappedBy="cables")
*/
private $couleurs;
` `
And my colors.php file:还有我的 colors.php 文件:
` `
/**
* @ORM\OneToMany(targetEntity=Cables::class, mappedBy="couleurs")
*/
private $cables;
public function __construct()
{
$this->cables = new ArrayCollection();
}
` `
Thanks for your help:)谢谢你的帮助:)
You have OneToMany
on both sides, that doesn't work.双方都有OneToMany
,这是行不通的。
I think you mean to do ManyToMany
where Colors
can be referenced by many Cables
and a Cable
can have many Colors
.我认为您的意思是做ManyToMany
,其中Colors
可以被许多Cables
引用,并且Cable
可以有很多Colors
。
Since you have a collection on each side and you manage the relation from Cable
, You should have a many-to-many-bidirectional relation with Cable
as the owning side.由于您在每一侧都有一个集合并且您管理来自Cable
的关系,因此您应该有一个以Cable
作为拥有方的多对多双向关系。 Try adjusting your mapping as follows.尝试如下调整映射。
Cable:电缆:
/**
* @ORM\ManyToMany(targetEntity=Colors::class, inversedBy="cables")
* @JoinTable(name="cables_colors")
*/
private $couleurs;
Colors: Colors:
/**
* @ORM\ManyToMany(targetEntity=Cables::class, mappedBy="couleurs")
*/
private $cables;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.