的Bonjour,
Je rencontreunproblèmevecsymfony danslacréationderelation entre mesn权。 Jesouhaiteréaliserunrelation ManyToOne entremonnocnéInstrunceetmonndentitUniversité。 Une题词estliéàuneuniversité。
谷歌翻译:
我在创建实体之间的关系时遇到了symfony的问题。我希望在我的身体和我的身体注册大学之间建立一个ManyToOne关系。铭文与大学相关联。
课堂题词:
/**
* Inscription
*
* @ORM\Table(name="Inscription")
* @ORM\Entity(repositoryClass="IEPA\PlatformBundle\Repository\InscriptionRepository")
*/
class Inscription
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=true)
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\ManyToOne(targetEntity="IEPA\PlatformBundle\Entity\Universites")
* @ORM\JoinColumn(nullable=false)
*/
private $univInsc;
}
班级大学:
/**
* Universites
*
* @ORM\Table(name="universites")
* @ORM\Entity
* @ORM\Entity(repositoryClass="IEPA\PlatformBundle\Repository\UniversitesRepository")
*/
class Universites
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
}
Quand je遇见了jour la basededonnée(php app / console doctrine:schema:update --force),j'obtient l'erreur suivante: 谷歌翻译:
当我更新数据库(php app / console doctrine:schema:update --force)时,我收到以下错误erreur
[Doctrine\DBAL\Exception\DriverException]
An exception occured while executing `ALTER TABLE inscription ADD CONTRAINT FK_D80C7901C5E240F6 FOREIGN KEY (univ_insc_id) REFERENCE universites (id):
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
答案 0 :(得分:1)
验证您是否已在Inscription实体中插入行。如果是,请删除他,或者如果您因为您的网站上线而无法删除它们。
更改此行:
@ORM\JoinColumn(nullable=false)
到
@ORM\JoinColumn(nullable=true)
并添加inversedBy ="铭文"这样的$ univInsc:
@ORM\ManyToOne(targetEntity="IEPA\PlatformBundle\Entity\Universites", inversedBy="inscriptions")
并尝试再次执行doctrine:update。
答案 1 :(得分:0)
萨吕,
Pourqueçafonctionneavec Doctrine,tudoisdéclarerlemanyToOne au niveau de tes inscriptions etducôtédetonUniversitél'verse(OneToMany)et je te conseille d'ajouter des annotations de joinColumn ainsi quelaréférenceàl'自动表。例如çadonne:
你好!为此,您需要在两个实体中声明关系。一个在ManyToOne,另一个在OneToMany。并说Doctrine是扭转数据的方法: /**
* Inscription
*
* @ORM\Table(name="Inscription")
* @ORM\Entity(repositoryClass="IEPA\PlatformBundle\Repository\InscriptionRepository")
*/
class Inscription
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=true)
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\ManyToOne(targetEntity="IEPA\PlatformBundle\Entity\Universites", inversedBy="inscriptions")
* @ORM\JoinColumn(name="idInscription", referencedColumnName="idInscription")
*/
private $univInsc;
}
Au niveau detonuniversité,on aura tes inscriptions quiserontréférencées:
在您的大学,我们添加了一系列铭文:
/**
* Universites
*
* @ORM\Table(name="universites")
* @ORM\Entity
* @ORM\Entity(repositoryClass="IEPA\PlatformBundle\Repository\UniversitesRepository")
*/
class Universites
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Collection
*
* @ORM\OneToMany(targetEntity="IEPA\PlatformBundle\Entity\Inscription", mappedBy="univInsc")
*/
private $inscriptions;
}
Autre选择:La convention de nommageveutgénéralementquelesAntitéssoientau singulier。 Tu peux les mettre au pluriel si tu le souhaites mais essaye de ne pas faire les deux comme ici。
Fais moi un retour si tu as une question sur ce que j'aifaitlà。 J'ai pas pris le temps d'êtresuperclairdésolé^^
一点点:命名约定是用单数字命名实体。你可以使用复数,但我建议你不要在同一个项目中使用它们。
如果您需要解释,请不要犹豫。
答案 2 :(得分:0)
您收到此错误是因为foreign key constraint
已存在。
尝试直接删除所拥有(多对一)方面的foreign key constraint
。删除后,重新运行 doctrine CLI命令以执行SQL更改。在ZF2中,那是php index.php orm:schema-tool:update --force
命令,它将重新创建索引。
答案 3 :(得分:0)
在为其他项目转储了一些表后,我遇到了这个问题,问题是排序规则。 utf8_unicode_ci中的旧表和utf8mb4_unicode_ci中的新表,因此我在doctrine.yaml中对其进行了更改。
发件人:
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
收件人:
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8
default_table_options:
charset: utf8
collate: utf8_unicode_ci
并...正在更新数据库架构...
5 queries were executed
[确定]数据库架构已成功更新!