Search code examples
doctrine-ormzend-framework2query-builder

Doctrine2 - Doctrine generating query with associated entity - InvalidFieldNameException


Yep, the title suggests: Doctrine is looking for a fieldname that's not there. That's both true and not true at the same time, though I cannot figure out how to fix it.

The full error:

File: D:\path\to\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractMySQLDriver.php:71

Message: An exception occurred while executing 'SELECT DISTINCT id_2 FROM (SELECT p0_.name AS name_0, p0_.code AS code_1, p0_.id AS id_2 FROM product_statuses p0_) dctrn_result ORDER BY p0_.language_id ASC, name_0 ASC LIMIT 25 OFFSET 0':

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'p0_.language_id' in 'order clause'

The query the error is caused by (from error above):

SELECT DISTINCT id_2
FROM (
       SELECT p0_.name AS name_0, p0_.code AS code_1, p0_.id AS id_2
       FROM product_statuses p0_
     ) dctrn_result
ORDER BY p0_.language_id ASC, name_0 ASC
LIMIT 25 OFFSET 0

Clearly, that query is not going to work. The ORDER BY should be in the sub-query, or else it should replace p0_ in the ORDER BY with dctrn_result and also get the language_id column in the sub-query to be returned.

The query is build using the QueryBuilder in the indexAction of a Controller in Zend Framework. All is very normal and the same function works perfectly fine when using a the addOrderBy() function for a single ORDER BY statement. In this instance I wish to use 2, first by language, then by name. But the above happens.

If someone knows a full solution to this (or maybe it's a bug?), that would be nice. Else a hint in the right direction to help me solve this issue would be greatly appreciated.

Below additional information - Entity and indexAction()

ProductStatus.php - Entity - Note the presence of language_id column

/**
 * @ORM\Table(name="product_statuses")
 * @ORM\Entity(repositoryClass="Hzw\Product\Repository\ProductStatusRepository")
 */
class ProductStatus extends AbstractEntity
{
    /**
     * @var string
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    protected $name;

    /**
     * @var string
     * @ORM\Column(name="code", type="string", length=255, nullable=false)
     */
    protected $code;

    /**
     * @var Language
     * @ORM\ManyToOne(targetEntity="Hzw\Country\Entity\Language")
     * @ORM\JoinColumn(name="language_id", referencedColumnName="id")
     */
    protected $language;

    /**
     * @var ArrayCollection|Product[]
     * @ORM\OneToMany(targetEntity="Hzw\Product\Entity\Product", mappedBy="status")
     */
    protected $products;

    [Getters/Setters]
}

IndexAction - Removed parts not directly related to QueryBuilder. Added in comments showing params as they are.

    /** @var QueryBuilder $qb */
    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb->select($asParam)              // 'pro'
        ->from($emEntity, $asParam);   // Hzw\Product\Entity\ProductStatus, 'pro'

    if (count($queryParams) > 0 && !is_null($query)) {
        // [...] creates WHERE statement, unused in this instance
    }

    if (isset($orderBy)) {  
        if (is_array($orderBy)) { 

            // !!! This else is executed !!!   <-----
            if (is_array($orderDirection)) { // 'ASC'
                // [...] other code
            } else {
                // $orderBy = ['language', 'name'], $orderDirection = 'ASC'

                foreach ($orderBy as $orderParam) {
                    $qb->addOrderBy($asParam . '.' . $orderParam, $orderDirection);
                }
            }
        } else {
            // This works fine. A single $orderBy with a single $orderDirection
            $qb->addOrderBy($asParam . '.' . $orderBy, $orderDirection);
        }
    }

================================================

UPDATE: I found the problem

The above issue is not caused by incorrect mapping or a possible bug. It's that the QueryBuilder does not automatically handle associations between entities when creating queries.

My expectation was that when an entity, such as ProductStatus above, contains the id's of the relation (i.e. language_id column), that it would be possible to use those properties in the QueryBuilder without issues.

Please see my own answer below how I fixed my functionality to be able to have a default handling of a single level of nesting (i.e. ProducStatus#language == Language, be able to use language.name as ORDER BY identifier).


Solution

  • Ok, after more searching around and digging into how and where this goes wrong, I found out that Doctrine does not handle relation type properties of entities during the generation of queries; or maybe does not default to using say, the primary key of an entity if nothing is specified.

    In the use case of my question above, the language property is of a @ORM\ManyToOne association to the Language entity.

    My use case calls for the ability to handle at lease one level of relations for default actions. So after I realized that this is not handled automatically (or with modifications such as language.id or language.name as identifiers) I decided to write a little function for it.

    /**
     * Adds order by parameters to QueryBuilder. 
     * 
     * Supports single level nesting of associations. For example:
     * 
     * Entity Product
     * product#name
     * product#language.name
     * 
     * Language being associated entity, but must be ordered by name. 
     * 
     * @param QueryBuilder  $qb
     * @param string        $tableKey - short alias (e.g. 'tab' with 'table AS tab') used for the starting table
     * @param string|array  $orderBy - string for single orderBy, array for multiple
     * @param string|array  $orderDirection - string for single orderDirection (ASC default), array for multiple. Must be same count as $orderBy.
     */
    public function createOrderBy(QueryBuilder $qb, $tableKey, $orderBy, $orderDirection = 'ASC')
    {
        if (!is_array($orderBy)) {
            $orderBy = [$orderBy];
        }
    
        if (!is_array($orderDirection)) {
            $orderDirection = [$orderDirection];
        }
    
        // $orderDirection is an array. We check if it's of equal length with $orderBy, else throw an error.
        if (count($orderBy) !== count($orderDirection)) {
    
            throw new \InvalidArgumentException(
                $this->getTranslator()->translate(
                    'If you specify both OrderBy and OrderDirection as arrays, they should be of equal length.'
                )
            );
        }
    
        $queryKeys = [$tableKey];
    
        foreach ($orderBy as $key => $orderParam) {
            if (strpos($orderParam, '.')) {
                if (substr_count($orderParam, '.') === 1) {
                    list($entity, $property) = explode('.', $orderParam);
                    $shortName = strtolower(substr($entity, 0, 3)); // Might not be unique...
                    $shortKey = $shortName . '_' . (count($queryKeys) + 1); // Now it's unique, use $shortKey when continuing
                    $queryKeys[] = $shortKey;
                    $shortName = strtolower(substr($entity, 0, 3));
                    $qb->join($tableKey . '.' . $entity, $shortName, Join::WITH);
    
                    $qb->addOrderBy($shortName . '.' . $property, $orderDirection[$key]);
                } else {
    
                    throw new \InvalidArgumentException(
                        $this->getTranslator()->translate(
                            'Only single join statements are supported. Please write a custom function for deeper nesting.'
                        )
                    );
                }
            } else {
                $qb->addOrderBy($tableKey . '.' . $orderParam, $orderDirection[$key]);
            }
        }
    }
    

    It by no means supports everything the QueryBuilder offers and is definitely not a final solution. But it gives a starting point and solid "default functionality" for an abstract function.