简体   繁体   English

Doctrine:不生成外键

[英]Doctrine : Foreign keys are not generated

The problem I have with doctrine is that after the 我与学说的问题在于

php bin/console doctrine:schema:update --force

it creates the DB but not the foreign keys. 它创建数据库但不创建外键。 I'm using annotations and I tried to use the @ORM\\JoinColumn in the ManyToOne ... etc annotations but without success. 我正在使用注释,我尝试在ManyToOne ...等注释中使用@ORM \\ JoinColumn但没有成功。

I hope you guys can help me. 我希望你们能帮助我。

Here is the code of one of my entities: 这是我的一个实体的代码:

<?PHP

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="member")
 */
class Member{

/**
 * @ORM\Column(type="string",length=25)
 * @ORM\Id
 */
private $code;

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

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

/**
 * @ORM\Column(type="integer")
 */
private $national_id;

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

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

/**
 * @ORM\Column(type="date")
 */
private $dob;

/**
 * @ORM\Column(type="integer")
 */
private $tel_mobile;

/**
 * @ORM\Column(type="integer")
 */
private $tel_home;

/**
 * @ORM\Column(type="integer")
 */
private $tel_ref;

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

/**
 * @ORM\Column(type="date")
 */
private $entry_date;

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

/**
 * @ORM\Column(type="integer",nullable=true)
 * @ORM\OneToOne(targetEntity="Staff")
 */
private $staff;

/**
 * @ORM\Column(type="integer",nullable=true)
 * @ORM\OneToOne(targetEntity="Student")
 */
private $student;

/**
 * @ORM\Column(type="integer")
 * @ORM\ManyToOne(targetEntity="Address")
 */
private $address;

/**
 * @ORM\Column(type="integer")
 * @ORM\ManyToOne(targetEntity="Faculty")
 */
private $faculty;

/**
 * @ORM\Column(type="integer")
 */
private $disable;

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

/**
 * @ORM\Column(type="integer",nullable=true)
 */
private $disable_year;

public function __construct()
{
    //nothing
}

// getters and setters

?>

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\\Column@ORM\\JoinColumn时,将忽略JoinColumn的关联,并且不会在表上创建外键。 so dont use both @ORM\\Column and @ORM\\JoinColumn in same column. 所以不要在同一列中同时使用@ORM\\Column@ORM\\JoinColumn

Do like below: 如下所示:

/**
*  @var Address
*
* @ORM\ManyToOne(targetEntity="Address")
* @ORM\JoinColumn(name="address_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $address;

It makes column named address_id on database table, with foreign key index and on deletion of Address record, associated record in dependent table is also deleted(cascade operation). 它在数据库表上创建名为address_id列,用外键索引和删除地址记录,依赖表中的相关记录也被删除(级联操作)。 You may user other operation on onDelete, see doctrine's documentation. 您可以在onDelete上使用其他操作,请参阅doctrine的文档。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM