[eluser]scherman[/eluser]
Hi people, i am developing a website, and i've just started with the Datamapper.
I have "Articles" and "Authors". The articles have one author, and the authors have one or more articles.
The problem is: I want to show an article's list with their authors.
Like:
Id: Article1->id
Name: Article1->title
Author: Article1->author->name
I do the next:
Controller
Code:
$article = new Article();
$data['articles'] = $article->get();
View
Code:
<h1>Count: <?php echo $articles->count(); ?></h1>
<?php foreach($articles->all as $article): ?>
<tr>
<td><?php echo $article->id ?></td>
<td><?php echo $article->title?></td>
<td><?php echo ucfirst($article->author->get()->name) ?></td>
</tr>
<?php endforeach ?>
but it generate a lot of queries:
- 1 query for the count()
- 1 query for the get()
- 1 query for each relation article-author
Is there any way to make it better with less queries? Because if i have 10 articles there'll be no problem, but if i'd have 10.000 articles, it'd very slowly!
And it is possible to make it with only one query if i write it.
Sorry for my english, i hope you can help me =)