Search code examples
phpsymfonydoctrine-ormdoctrinedql

When I use the WHERE clause my basic entity values are NULL


I want to refac a native SQL query with the doctrine querybuilder. In this case I get the following error: The query executes and returns a result (with and without joins). But when I add a simple WHERE the basic entity values are NULL.

Tested
- QueryBuilder (complex: as default I need six joins)
- QueryBuilder (simple: no join)
- HQL
- different getResult() and execute() calls

Versions:

PHP 7.0 (no updates possible ...)

 ./composer.phar info | grep doctrine
doctrine/annotations                 v1.4.0   Docblock Annotations Parser
doctrine/cache                       v1.6.2   Caching library offering an object-oriented API for many cache backends
doctrine/collections                 v1.4.0   Collections Abstraction library
doctrine/common                      v2.7.3   Common Library for Doctrine projects
doctrine/dbal                        v2.5.13  Database Abstraction Layer
doctrine/doctrine-bundle             1.10.3   Symfony DoctrineBundle
doctrine/doctrine-cache-bundle       1.3.5    Symfony Bundle for Doctrine Cache
doctrine/inflector                   v1.2.0   Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator                1.0.5    A small, lightweight utility to instantiate objects in PHP without invoking their cons...
doctrine/lexer                       1.0.2    PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Pars...
doctrine/orm                         v2.5.14  Object-Relational-Mapper for PHP

Some code

The basic values are filled (like id and title).

$qb = $this->createQueryBuilder('artikel')
           ;
return $qb->getQuery()->execute();

The basic values are null except the id.

$qb = $this->createQueryBuilder('artikel')
           ->andWhere('artikel.id = :ids')
           ->setParameter('ids', '1010729320')
           ;
return $qb->getQuery()->execute();

Why are the basic value null when I use the WHERE clause?


Solution

  • It's very easy. ;)

    Problem was

    The datatype of the id is an integer and the given param has type string.
    So it differs and php does no auto-cast something.

    Solution

    Remove the ' before and after the id.

    The correct code

    Note: spot the missing '

    $qb = $this->createQueryBuilder('artikel')
               ->andWhere('artikel.id = :ids')
               ->setParameter('ids', 1010729320)
               ;
    return $qb->getQuery()->execute();
    

    For me the problem occures before, but this is an other story (I write some cents in the comment above).

    Thank you all