Doctrine2 Native Query: Rows are shifted

462 views Asked by At

I want to use Doctrine result mapping, but I get a very strange result.

This is my code in the repository "Result":

    $sql = 'SELECT * FROM (
                SELECT
                k.id AS keyword_id,
                k.name,
                r.position,
                r.date,
                r.id
                FROM
                Result r
                INNER JOIN Keyword k ON r.keyword_id = k.id
                WHERE r.domain_id = 2005
                ORDER BY
                date DESC
            ) AS foo
            GROUP BY
            CONCAT(name, DATE_FORMAT(date, \'%Y%m\') )
            ORDER BY
            CONCAT(name, date) ASC;';

    $rsm = new ResultSetMapping;
    $rsm->addEntityResult('XXX\SesBundle\Entity\Result', 'r');
    $rsm->addFieldResult('r', 'id', 'id');
    $rsm->addFieldResult('r', 'position', 'position');
    $rsm->addFieldResult('r', 'date', 'date');
    $rsm->addJoinedEntityResult('XXX\SesBundle\Entity\Keyword', 'k', 'r', 'keyword');
    $rsm->addFieldResult('k', 'keyword_id', 'id');
    $rsm->addFieldResult('k', 'name', 'name');

    $em = $this->getEntityManager();

    $results = $em->createNativeQuery($sql, $rsm)
                ->getResult();

    return $results;

There has to be 20 results and if I count($results) the result is 20.

But If I var_dump($result[19]) I get this dump:

  [19] =>
  class Example\SesBundle\Entity\Result#1565 (7) {
    private $id =>
    int(498)
    private $url =>
    NULL
    private $position =>
    int(30)
    private $date =>
    class DateTime#1594 (3) {
      public $date =>
      string(19) "2012-10-02 12:04:17"
      public $timezone_type =>
      int(3)
      public $timezone =>
      string(13) "Europe/Berlin"
    }
    private $keyword =>
    NULL
    private $domain =>
    NULL
    private $engine =>
    NULL
  }
}

Look, private $keyword is NULL. A proper dump should look like this:

  [16] =>
  class Example\SesBundle\Entity\Result#1623 (7) {
    private $id =>
    int(19)
    private $url =>
    NULL
    private $position =>
    int(2)
    private $date =>
    class DateTime#1619 (3) {
      public $date =>
      string(19) "2012-09-28 17:33:26"
      public $timezone_type =>
      int(3)
      public $timezone =>
      string(13) "Europe/Berlin"
    }
    private $keyword =>
    class Example\SesBundle\Entity\Keyword#1647 (3) {
      private $id =>
      int(2376)
      private $name =>
      string(19) "Example"
      private $domain =>
      class Doctrine\ORM\PersistentCollection#1624 (9) {
        ...
      }
    }
    private $domain =>
    NULL
    private $engine =>
    NULL
  }

Also there is another very strange behaviour. The results are "shifted". The result which is supposed to be $result[0] doesn't appear in the var_dump(). If the code is executed $result[0] is the result which is supposed to be $result[1], but has the id of the result which is supposed to be $result[0]. So I think the mapping isn't quite right.

If I execute the SQL Statement in Navicat everything is quite right.

Sorry for my bad English, I hope you understand what I mean :)

1

There are 1 answers

0
user1731323 On BEST ANSWER

Here's the solution:

When using addJoinedEntityResult you HAVE to do the mapping in the same order as you select the columns in the sql statement.

For further information please take a look at: http://www.doctrine-project.org/jira/browse/ddc-1362

I don't know why the doctrine team closed the ticket.

Thanks to @luka8088 for this solution.