简体   繁体   English

Symfony 3.1 Doctrine实体无法更改表添加外键

[英]Symfony 3.1 Doctrine Entity Cannot alter table add foreign key

Symfony 3.1 - Doctrine : I am trying to add relationships between entities and thereby want to add foreign key to link tables in my example. Symfony 3.1-主义 :我试图在实体之间添加关系,因此想在示例中添加外键来链接表。 I tried several settings, utilizing various combination of arguments, but none of them were able to ALTER the tables with Foreign key. 我尝试了几种设置,利用了各种参数组合,但没有一个能够用外键更改表。

My entities are namely "Trips" and "Airlines" with the content attached below. 我的实体是“ Trips”和“航空公司”,其内容如下。 I would like to use airline_id as a foreign key inside trips table. 我想将travel_id用作行程表中的外键。 Here are the extract from my entity scripts: 以下是我的实体脚本的摘录:

Entity#1: Trips 实体1:行程

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="trips")
 */

class Trips
{

    /**
     * @ORM\Column(name="trip_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $trip_id;

    /**
     * @ORM\Column(name="airline_id", type="integer")
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Airlines")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="airline_id", referencedColumnName="airline_id")
     * })
     */
    private $airline_id;

}

Entity#2: Airlines 实体#2:航空公司

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="airlines")
 */

class Airlines
{

    /**
     * @ORM\Column(name="airline_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $airline_id;

    /**
     * @ORM\Column(type="string", length=80)
     */
    private $name;

}

I tried updating the schema from console , but all it says is 我尝试从控制台更新架构,但它只说明了

Nothing to update - your database is already in sync with the current entity metadata. 无需更新-您的数据库已经与当前实体元数据同步。

Also tried deleting the table and re-creating them with console; 还尝试删除表并使用控制台重新创建它们; the result did not change much and the foreign key is still missing. 结果没有太大变化,并且外键仍然丢失。 :/ :/

Any thoughts, or perhaps you know the solution? 有什么想法,或者您知道解决方案吗?

I have a few suggestions, as I've gone through the same process of design. 我已经提出了一些建议,因为我经历了相同的设计过程。 First off, should I presume that "a trip has one airline" and also "an airline has many trips"? 首先,我是否应该假设“一次旅行有一家航空公司”以及“一次航空公司有很多航班”? This seems logical, and since you show @ORM\\ManyToOne annotation for the Trips class, that would equate to my same presumptions. 这似乎是合乎逻辑的,并且由于您为Trips类显示@ORM \\ ManyToOne批注,因此这与我的假设相同。

If this is so, then these are the suggestions: 如果是这样,那么建议如下:

First, don't call the classes "Trips" and "Airlines", but rather in the singular, that is "Trip" and "Airline". 首先,不要将类称为“ Trips”和“ Airlines”,而应使用单数形式,即“ Trip”和“ Airline”。 This is object oriented thinking. 这是面向对象的思维。 So when you create a "Trip" Doctrine object, it represents a trip for example. 因此,当您创建“旅行”学说对象时,例如,它表示旅行。 This also makes you code more readable and also supportable from a future standpoint; 从将来的角度来看,这也使您的代码更具可读性和支持性。 as because when people read your code, they will understand that a single instance of a Trip (from the Trip class), represents a trip (that a person takes). 因为当人们阅读您的代码时,他们会理解Trip的一个实例(来自Trip类)代表一个旅程(一个人参加)。 This is important when designing code. 这在设计代码时很重要。

Given the above, make these code changes. 鉴于以上所述,请对这些代码进行更改。

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="trip")
 */
class Trip
{
    /**
     * @ORM\Id
      * @ORM\Column(name="trip_id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $trip_id;

    /**
     * @ORM\ManyToOne(targetEntity="Airline", inversedBy="trips")
     * @ORM\JoinColumn(name="trip_airline_id", referencedColumnName="airline_id")
     */
    private $airline;
     ...
     /**
      * Set airline - Adds this trip to the passed in airline and sets the airline
      * property to resultant airline.
      * 
      * @param \AppBundle\Entity\Airline
      */
     public function setAirline($airline){
         $airline->addTrip($this);
         $this->airline = $airline;
     }
}

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="airlines")
 */
class Airlines
{
    /**
     * @ORM\Column(name="airline_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $airline_id;

    /**
     * @ORM\Column(type="string", length=80)
     */
    private $name;

     /**
      * @ORM\OneToMany(targetEntity="Trip", mappedBy="airline")
      */
     protected $trips = null;
    ... 
     public function __construct(){
        // This is an array of trips.
        $this->trips = new ArrayCollection();
    }
    ...
    /**
     * Add trip - Adds a course to the array.
     */
    public function addTrip($trip){
        $this->trips[] = $trip;
    }
}

Now you can get trips from the airline, and also you can do the other way around, get the airline from the trip. 现在,您可以从航空公司出发,也可以通过其他方式从旅途中选择航空公司。

When @ORM\\Column is specified along with @ORM\\JoinColumn on same column, then JoinColumn's association gets ignored and Foreign Key isn't created on table. 当在同一列上同时指定@ORM\\Column@ORM\\JoinColumn时,JoinColumn的关联将被忽略,并且不会在表上创建外键。 so dont use both @ORM\\Column and @ORM\\JoinColumn in same column. 因此, @ORM\\JoinColumn在同一列中同时使用@ORM\\Column@ORM\\JoinColumn There is no need to specify data type with @ORM\\Column for associations, doctrine handles it and assign data type based on database engine. 无需使用@ORM\\Column指定数据类型进行关联,使用主义进行处理并根据数据库引擎分配数据类型。 For MySql, it is int. 对于MySql,它是int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sqlite 平台不支持更改外键(Symfony,学说迁移) - Sqlite platform does not support alter foreign key ( Symfony, doctrine migration ) 主义2.2 / Symfony 2.2:使用复合外键持久化实体 - Doctrine 2.2/Symfony 2.2: Persisting an entity with a composite foreign key Symfony / Doctrine类表继承和外键作为主键 - Symfony/Doctrine class table inheritance and foreign key as primary key MySQL错误代码1215:无法在ALTER TABLE上添加外键约束:Mysql - MySQL Error Code 1215: Cannot add foreign key Constraint on ALTER TABLE : Mysql SQLSTATE[HY000]: 一般错误: 1215 无法添加外键约束 (SQL: alter table - SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table Symfony 4 Doctrine 2,映射实体,错误表没有主键, - Symfony 4 Doctrine 2, Mapping entity, error table has no primary key, 主义2 - 持有实体与加入表的外键反对外国实体 - Doctrine 2 - Persist Entity with Joined Table's Foreign Key Opposed to Foreign Entity Symfony:将列添加到原则实体 - Symfony: Add Column to Doctrine Entity 如何在不破坏实体管理器的情况下忽略 doctrine/symfony 中与外键相关的异常? - How to ignore foreign key related exceptions in doctrine/symfony without having the entity manager destroyed? 在Doctrine / Symfony中,外键未与OneToMany关系保存 - Foreign key not saving with OneToMany relationship in Doctrine/Symfony
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM