Fanda <d...@sidak.net> wrote:

> how to solve this problem:
> 
> If I write a repository for each entity, article and autor for
> example, article is composed of autor and some other things, then I
> want to select some articles: 
> 
> $articles = $articleRepo->findLast(10);
> foreach ($articles as $article) {
>     // echo article content
>     ...
>     // echo
>     $article->getAutor(); // next query into database, in each cycle
> }
> 
> Is some elegant way, how to do this (optimise)?

The full extent of doing this is way beyond an email list. Not knowing
how much you already know about object-oriented programming PHP or in
general, it's a bit hard to guide you. Also, it's a bit unclear whether
you're asking about how to implement the database, the interaction with
the database, writing PHP classes, or what, exactly.

Certainly, what you've outlined above is quite possible in PHP, and from
a mainline code point-of-view, fairly concise. The heavy lifting will be
done in the classes you set up for the article repository itself. From
what you've said above, there are at least four classes you'll have to
write:

1. an article repository class
2. an article class
3. an author class
4. an entity class (although it's in no way clear what you mean by
entity from the above description.)

It will be important to define relationships between these classes, from
what little I can gleen, the following looks like it might work:

* An article repository has many articles
* An article has one or many authors
* Still no clue how entity relates to articles or authors

You'll need to design data base tables that correspond to these classes
as well.

There is potentially another class you could write which deals with all
the underlying details of the database interaction itself, but you
should look at PDO[1] before you go about writing one yourself. PDO may
be all you need for this.

You might avail yourself of a framework that deals with database objects
under the hood rather than write all this yourself as well, although
doing that will probably not be as instructive as writing your own. The
decision may rest with how much time you have to invest in learning
what. It's not an exact tradeoff, because all the frameworks also
require a learning curve to use them.

[1] http://us.php.net/manual/en/book.pdo.php

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to