Kdyby doctrine – postFetch
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.

- Tanadche
 - Člen | 11
 
Ahoj,
já osobně využívám toho, že metoda postFetch v QueryObject volá event
onPostFetch (https://github.com/…ryObject.php#…).
Do QueryObjectu pak jenom přidám metody, které nastaví callback na
onPostFetch event.
Ve tvém případě by metody mohly vypadat přibližně takhle:
public function withComments()
{
	$this->onPostFetch[] = function (QueryObject $queryObject, Queryable $repository, \Iterator $iterator)
	{
		$ids = iterator_to_array($iterator, true);
		$repository->createQueryBuilder()
			->select('PARTIAL article.{id}, comment')->from(Article::getClassName(), 'article')
			->leftJoin('article.comments', 'comment')
			->andWhere('article.id IN (:articles)')->setParameter('articles', $ids)
			->getQuery()->getResult();
	};
}
public function withAuthors()
{
	$this->onPostFetch[] = function (QueryObject $queryObject, Queryable $repository, \Iterator $iterator)
	{
		$ids = iterator_to_array($iterator, true);
		$repository->createQueryBuilder()
			->select('PARTIAL article.{id}, author')->from(Article::getClassName(), 'article')
			->leftJoin('article.authors', 'author')
			->andWhere('article.id IN (:articles)')->setParameter('articles', $ids)
			->getQuery()->getResult();
	};
}
Neříkám, že je to best-practice, ale tento způsob mi zatím vyhovuje.

- pitr82
 - Člen | 121
 
@Tanadche Super díky, funguje super.
Takto si krásně vypíšu články
{foreach $articles as $article}
		<h1>Article:{$article->title}</h1>
		    <h3>Author:</h3><p>{$article->author->name}</p>
		<h3>Comments:</h3>
		{foreach $article->comments as $comment}
		<p>{$comment->content}</p>
		{/foreach}
{/foreach}
Ale mě zajímalo spíše toto, kdybych chtěl vypsat stromově, všechny autory, jejich články a komentáře ke článkům.
{foreach $authors as $author}
		<h1>Authors:</h1>
		<h2>{$author->id}, {$author->name}</h2>
		{foreach $author->articles as $article}
		    <h3>Article:{$article->title}</h3>
		    <h3>Comments:</h3>
		    {foreach $article->comments as $comment}
		    <p>{$comment->content}</p>
		    {/foreach}
		{/foreach}
{/foreach}
Rozložit toto pomocí postFetch
$this->em->createQueryBuilder()
			->addSelect("a")
			->from(Model\Entities\Author::class, "a", "a.id")
			->addOrderBy("a.name", "ASC")
			->leftJoin('a.articles', 'article')->addSelect('article')
		    ->leftJoin('article.comments', 'comments')->addSelect('comments')
		    ->getQuery()->getResult();