0

I have an archive table (id, title, author) and a download_history table (id, id_archive, date) to keep track of how many download for each archive. Now I want to build a report to view the most downloaded archives on a weekly bases. Pretty simple to do in SQL, but things are more complicated with doctrine, this is what I've done so far:

public function getTopDownloadsLastXDays($limit=100, $days=7)
{
    $rsm = new ResultSetMapping();
    $rsm->addEntityResult('DownloadHistory', 'd');
    $rsm->addMetaResult('d', 'archive', 'archive');
    $rsm->addScalarResult('tot', 'tot');
    $rsm->addJoinedEntityResult('Archive', 'a', 'd', 'archive');
    $rsm->addFieldResult('a', 'author', 'author');
    $rsm->addFieldResult('a', 'title', 'title');

    $sql = "SELECT
              DISTINCT(dh.archive_id),
              COUNT(*) as tot,
              a.author,
              a.title
            FROM
              download_history dh
            INNER JOIN
              archive a
            ON
              dh.archive_id = a.id
            WHERE
              dh.date >= DATE(NOW()) - INTERVAL :days DAY
            GROUP BY
              dh.archive_id
            LIMIT :limit";

    $em = $this->getEntityManager();
    $query = $em->createNativeQuery($sql, $rsm);
    $query->setParameters(array(
        ':days'     =>  $days,
        ':limit'    =>  $limit,
    ));

    $results = $query->getResult();
    return $results;
}

Entities exist and work fine. I followed the third example here: http://doctrine-orm.readthedocs.org/en/latest/reference/native-sql.html#examples. This is my result:

array(1) { 
    [-1]=> array(1) { 
        ["tot"]=> string(1) "1" 
    } 
}

any idea? thanks

2
  • you tested the query in mysql ? does it work as expected? Commented May 15, 2015 at 3:39
  • yes if I execute it on the command line it works as expected. Commented May 15, 2015 at 9:47

1 Answer 1

0

You aliased the table as "dh", but you specified the alias as "d" when building the ResultSetMapping.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.