我在 Doctrine 上遇到外键问题。我有一个“用户”表和一个“订单”表。现在我想要一个从 orders-table 中的 userID 到 user-table 中的 id 的外键。订单实体看起来像:
<?php
namespace Application\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(
* name="Orders",
* options={"collate"="utf8_general_ci",
* "charset"="utf8",
* "engine"="InnoDB"
* }
* )
* @ORM\Entity(repositoryClass="Application\TestBundle\Entity\OrdersRepository")
*/
class Orders
{
/**
* @var integer
*
* @ORM\Column(
* name="id",
* unique=true,
* type="integer",
* options={"unsigned"=true}
* )
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(
* name="userID",
* type="integer",
* nullable=false,
* options={"unsigned"=true}
* )
* @ORM\OneToOne(targetEntity="User")
* @ORM\JoinColumn(name="userID", referencedColumnName="id")
*/
private $userID;
// ...
}
用户实体看起来像:
<?php
namespace Application\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(
* name="User",
* options={"collate"="utf8_general_ci",
* "charset"="utf8",
* "engine"="InnoDB"
* }
* )
* @ORM\Entity(repositoryClass="Application\TestBundle\Entity\UserRepository")
*/
class User
{
/**
* @var integer
*
* @ORM\Column(
* name="id",
* unique=true,
* type="integer",
* options={"unsigned"=true}
* )
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
// ...
}
不幸的是不会设置外键。它不在 SQL 查询中。我做错了什么?
最佳答案
解决方法很简单。它只能是关系或数据类型(字符串、对象、整数)。所以我的例子需要一个关系,所以我不得不删除 @ORM\Column
-annotation 并且它起作用了!
关于mysql - Doctrine 创建外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24209196/