问题
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