使用条件和多对多关系的查询中的字段名错误 [英] Wrong fieldname in query with Criteria and Many-To-Many-Relation
问题描述
当我尝试对多对多关系中具有不同列名的属性使用简单条件时,Doctrine使用属性名作为字段,而不是列名.
When I try to use a simple Criteria on a property with a different columnname in Many-To-Many-Relation, Doctrine uses the propertyname as the field and not the columnname.
人员ORM定义
...
manyToMany:
attributes:
targetEntity: Attributes
cascade: ['persist']
joinTable:
name: person_attribute
joinColumns:
person_id:
referencedColumnName: id
inverseJoinColumns:
attribute_id:
referencedColumnName: id
...
具有不同列名的属性ORM定义
Attribute ORM Definition with differing columnname
...
name:
type: string
nullable: false
length: 50
options:
fixed: false
column: '`key`'
...
Person :: hasAttribute()-方法
Person::hasAttribute()-Method
$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"类的函数"loadCriteria()"中,位于以下行: >
The issue is in "lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php" class, in function "loadCriteria()" at lines:
foreach ($parameters as $parameter) {
list($name, $value) = $parameter;
$whereClauses[] = sprintf('te.%s = ?', $name);
$params[] = $value;
}
已在 github链接和 href ="https://github.com/doctrine/doctrine2/pull/5668" rel ="nofollow noreferrer">拉动请求
如您所见,以上代码已更改为:
As you can see, the above code was changed to:
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版本的一部分. 我建议您使用master分支中的代码.
Current releases do not use this fix. It will be part of 2.6 release. I suggest you use the code in the master branch.
更新:自版本2.6 .
这篇关于使用条件和多对多关系的查询中的字段名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!