Doctrine 2, Getting the last n articles for each category

拟墨画扇 提交于 2020-01-17 02:53:34

问题


I have 2 entities, Article and Category, with a ManyToOne relation owned by Article and i want is to get the last 4 articles for each category.

If i was working with MySQL, the solution would be here "Retrieve 2 last posts for each category", but unfortunately i'm working with DOCTRINE2 and DQL, I really don't know how to translate the two query in the answer to get the same result with DOCTRINE2.

Thanks in advance to whom can help me with this.


回答1:


If working with Doctrine Query Language or Query Builder will be difficult to you, then you can write your pure query and call it in your repository class like this:

public function fetchSomeRecordFromMyTable()
{
    $sql = 'MY PURE MYSQl QUERY';

    $connection = $this->getEntityManager()->getConnection()->prepare($sql);
    $connection->execute();

    return $connection->fetchAll(\PDO::FETCH_ASSOC);
}


来源:https://stackoverflow.com/questions/24979351/doctrine-2-getting-the-last-n-articles-for-each-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!