我正在使用 Symfony 2.1.2。
我有两个实体并在它们之间定义 [多对一(双向)] (1) 关联。我不想将主键用作外键(referencedColumnName)。我想使用另一个整数唯一列:customer_no
/**
* @ORM\Entity
* @ORM\Table(name="t_myuser")
*/
class MyUser extends BaseEntity // provides an id (pk)
{
/**
* @ORM\ManyToOne(targetEntity="Customer", inversedBy="user")
* @ORM\JoinColumn(name="customer_no", referencedColumnName="customer_no", nullable=false)
*/
public $customer;
}
/**
* @ORM\Entity
* @ORM\Table(name="t_customer")
*/
class Customer extends BaseEntity // provides an id (pk)
{
/**
* @ORM\Column(type="integer", unique=true, nullable=false)
*/
public $customer_no;
/**
* @ORM\OneToMany(targetEntity="MyUser", mappedBy="customer")
*/
public $user;
}
当我尝试将 MyUser 实体与 Customer 实体持久化时,我收到此错误:
注意:未定义索引:customer_no in ...\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php 行 608
db 上的架构看起来不错,这些应该是重要的 sql 架构定义:
CREATE UNIQUE INDEX UNIQ_B4905AC83CDDA96E ON t_customer (customer_no);
CREATE INDEX IDX_BB041B3B3CDDA96E ON t_myuser (customer_no);
ALTER TABLE t_myuser ADD CONSTRAINT FK_BB041B3B3CDDA96E FOREIGN KEY (customer_no)
REFERENCES t_customer (customer_no) NOT DEFERRABLE INITIALLY IMMEDIATE;
所以肯定有customer_no的索引
//更新: 我修复了 inversedBy 和 mappedBy 的东西,但这不是问题。