学说2,获取每个类别的最后n篇文章 [英] Doctrine 2, Getting the last n articles for each category
问题描述
如果我正在与MySQL合作,解决方案将在这里检索每个类别的最后两个帖子,但不幸的是,我正在使用DOCTRINE2和DQL,我真的不知道如何在答案中翻译两个查询以获得与DOCTRINE2相同的结果。
提前感谢谁能帮助我。
如果使用Doctrine查询语言或查询生成器将很难你,那么你可以编写你的纯粹的查询,并在你的仓库类中调用它:
public function fetchSomeRecordFromMyTable ()
{
$ sql ='MY PURE MYSQl QUERY';
$ connection = $ this-> getEntityManager() - > getConnection() - > prepare($ sql);
$ connection-> execute();
return $ connection-> fetchAll(\PDO :: FETCH_ASSOC);
}
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.
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);
}
这篇关于学说2,获取每个类别的最后n篇文章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!