Welcome Guest, Not a member yet? Register   Sign In
DataMapper: Too many queries.
#1

[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: &lt;?php echo $articles->count(); ?&gt;</h1>
&lt;?php foreach($articles->all as $article): ?&gt;
    <tr>
        <td>&lt;?php echo $article->id ?&gt;</td>
        <td>&lt;?php echo $article->title?&gt;</td>
        <td>&lt;?php echo ucfirst($article->author->get()->name) ?&gt;</td>
    </tr>
&lt;?php endforeach ?&gt;

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 =)




Theme © iAndrew 2016 - Forum software by © MyBB