学说2,获取每个类别的最后n篇文章 [英] Doctrine 2, Getting the last n articles for each category

查看:129
本文介绍了学说2,获取每个类别的最后n篇文章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个实体,文章和类别,拥有文章所拥有的ManyToOne关系,我想要得到每个类别的最后4篇文章。



如果我正在与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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆