Invalid field name in query with criteria and many-to-many relationship

When I try to use simple criteria for a property with a different column name in many ways for many, Doctrine uses the property name as a field, not a column name.

Face ORM Definition

...
manyToMany:
    attributes:
        targetEntity: Attributes
        cascade: ['persist']
        joinTable:
            name: person_attribute
            joinColumns:
                person_id:
                    referencedColumnName: id
            inverseJoinColumns:
                attribute_id:
                    referencedColumnName: id
...

Defining an ORM attribute with a different column name

...
name:
    type: string
    nullable: false
    length: 50
    options:
        fixed: false
    column: '`key`'
...

Person :: hasAttribute () - method

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

if ($this->getAttributes()->matching($criteria)->first()) {
    return true;
}

Generated statement

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` = ?"
+4
source share
1 answer

The problem is the class "lib / Doctrine / ORM / Persisters / Collection / ManyToManyPersister.php" in the function "loadCriteria ()" in the lines:

foreach ($parameters as $parameter) {
        list($name, $value) = $parameter;
        $whereClauses[]     = sprintf('te.%s = ?', $name);
        $params[]           = $value;
}

fix was added to github link and pull request

As you can see, the above code has been 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. .

+1

Source: https://habr.com/ru/post/1688132/


All Articles