duanpao4172 2017-04-24 04:03
浏览 48
已采纳

Doctrine:不生成外键

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.

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

?>
  • 写回答

1条回答 默认 最新

  • duan1979768678 2017-04-24 06:34
    关注

    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. so dont use both @ORM\Column and @ORM\JoinColumn in same column.

    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). You may user other operation on onDelete, see doctrine's documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘