Doctrine orm 查询条件和多对多关系中的字段名错误

Doctrine orm 查询条件和多对多关系中的字段名错误,doctrine-orm,orm,criteria,relation,Doctrine Orm,Orm,Criteria,Relation,当我尝试对多对多关系中具有不同列名的属性使用简单条件时,原则使用propertyname作为字段,而不是列名 人员ORM定义 ... manyToMany: attributes: targetEntity: Attributes cascade: ['persist'] joinTable: name: person_attribute joinColumns:

当我尝试对多对多关系中具有不同列名的属性使用简单条件时,原则使用propertyname作为字段,而不是列名

人员ORM定义

...
manyToMany:
    attributes:
        targetEntity: Attributes
        cascade: ['persist']
        joinTable:
            name: person_attribute
            joinColumns:
                person_id:
                    referencedColumnName: id
            inverseJoinColumns:
                attribute_id:
                    referencedColumnName: id
...
具有不同列名称的属性ORM定义

...
name:
    type: string
    nullable: false
    length: 50
    options:
        fixed: false
    column: '`key`'
...
Person::hasAttribute方法

$criteria = Criteria::create()
    ->where(Criteria::expr()->eq('name', $attributeName))
    ->setFirstResult(0)
    ->setMaxResults(1);

if ($this->getAttributes()->matching($criteria)->first()) {
    return true;
}
生成的语句

SELECT 
    te.id AS id, 
    te.description AS description, 
    te.key AS key 
FROM 
    attribute te 
JOIN 
    person_attribute t 
ON 
    t.attribute_id = te.id 
WHERE 
    t.person_id = ? 
        AND 
    te.name = ?     ## <- This should be "te.`key` = ?"

问题出现在lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php类的函数装入条件中,第行:

foreach ($parameters as $parameter) {
        list($name, $value) = $parameter;
        $whereClauses[]     = sprintf('te.%s = ?', $name);
        $params[]           = $value;
}
修复程序已添加到和

如您所见,上述代码已更改为:

foreach ($parameters as $parameter) {
        list($name, $value) = $parameter;
        $field = $this->quoteStrategy->getColumnName($name, $targetClass, $this->platform);
        $whereClauses[]     = sprintf('te.%s = ?', $field);
        $params[]           = $value;
}
当前版本不使用此修复程序。 这将是2.6版本的一部分。 我建议您使用主分支中的代码


更新:修复程序从开始可用。

当我将条件更改为键时,它会在Doctrine必须从DB加载关系时起作用,但会在我的单元测试中导致PHP致命错误,因为我的实体没有属性键或getKey。您的访问权限是否设置为相应类上的属性?默认情况下,访问是字段?您的ORM版本是什么?