问题描述
我正在使用Symfony 2.1.2。
I'm using Symfony 2.1.2.
我有两个实体,并在它们之间定义了[多对一(双向)](1)关联。我不想将主键用作外键(referencedColumnName)。我想使用另一个整数唯一列:customer_no
I have two entities and define a [many-to-one (bidirectional)] (1) association between them. I don't want to use the primary key for the foreign key (referencedColumnName). I want to use another integer unique column: 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实体保持在一起时,我得到了错误:
When I try to persist a MyUser entity with an Customer entity, I get this error:
注意:未定义索引:customer_no in ...供应商\doctrine\orm\lib\Doctrine\ORM\Persisters\ BasicEntityPersister.php第608行
Notice: Undefined index: customer_no in ...\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php line 608
数据库上的架构看起来不错,这些应该是重要的sql架构定义:
The schema on the db looks fine, these should be the important sql schema definitions:
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和mappingBy的问题,但这不是问题。
//update:I fix the inversedBy and mappedBy stuff, but this is not the problem.
(1):
推荐答案
@ m2mdas:
是的'是的,我认为这是可能的,因为。属性 referencedColumnName
仅适用于您的属性与表列不匹配的情况。
@m2mdas:
Yes you're right, I thought it's possible because JPA (which has influence to doctrine) has this feature. The attribute referencedColumnName
only for the case when your property does not match the table column.
无论如何,我发现一个通过修补BasicEntityPersister.php解决方案,请参见github上的要点:
Whatever, I found a solution by patching the BasicEntityPersister.php, see here the gist on github: https://gist.github.com/3800132
解决方案是为映射列添加属性/字段名称和值。此信息已经存在,但未绑定到正确的位置。必须以这种方式将其添加到 $ newValId :
the solution is to add the property/field name and value for the mapping column. This information is already there but not bound to the right place. It have to be added to the $newValId arrray this way:
$fieldName = $targetClass->getFieldName($targetColumn);
$newValId[$fieldName] = $targetClass->getFieldValue($newVal, $fieldName);
仅适用于ManyToOne参考。 ManyToMany不起作用。
对于ManyToOne,我已经使用现有实体对其进行了测试。您也可以对其进行测试:
It only works for ManyToOne reference. ManyToMany doesn't work.
For ManyToOne I test it with already existing entities. You can test it, too:
在 tests / Doctrine / Tests / Models / Legacy / LegacyArticle.php $ c中更改主义注释$ c>
来自
change the doctrine annotation in tests/Doctrine/Tests/Models/Legacy/LegacyArticle.php
from
@JoinColumn(name="iUserId", referencedColumnName="iUserId")
到
@JoinColumn(name="username", referencedColumnName="sUsername")
这篇关于doctrine2:未定义的索引-具有非默认referencedColumnName的多对一实体不会持久存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!