Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to get all children when it's filtered by children field?
  • Let we have 2 tables Model\Author has_many Model\Article and it has a field 'category_id'. I need to found all Authors that writes some Article in certain category, but later I want to have access to all children of every Author.

    I made this query:
    $authors = Model\Author::query()
        ->related('articles')
        ->where('articles.category_id', '5')
        ->get();

    foreach ($authors as $author) {
        foreach ($author->articles as $article) {
          // I have only articles that belongs to category with id = 5
       }
    }

    How can I get all articles for every author? Is there only way to make this query inside the first level loop?
    $articles = Model\Article::find('all', ['where' => ['author_id', $author->id]]);

    Is not there any way to make something like this:
    $author->articles->load() or $author->load('articles')?

    I don't hope to get some way to get all I want with one query at all...
  • I not sure I understand your question.

    If you want all authors, with all articles, simply do:

    $authors = Model\Author::query()
        ->related('articles')
        ->get();

    Or if you want all articles with author info, use

    $articles = Model\Article::query()
        ->related('author')
        ->get();

  • No, not all authors, but only who wrote any article in certain category, e. g. with category_id = 5
    And all articles for those authors. I show category articles list and I want to show "other articles of this author".

    I think, only way to do this is to use query builder and subquery. Select all authors ids who wrote in ceratain category, and select all articles that has author_id in previously selected list of ids.
  • If you want to show all articles in category 5, you start with

    $articles = Model\Article::query()
        ->related('author')
        ->related('category')
        ->where('category_id', '5')
        ->get();

    If you then want to know all articles written by all selected authors, you could extend this to

    $articles = Model\Article::query()
        ->related('author')
        ->related('author.article')
        ->related('category')
        ->where('category_id', '5')
        ->get();

    Assuming you have a one-to-many relation between author and article called "article".

    This will allow you to do

    foreach ($articles as $article)
    {
        $other_articles = $article->author->article;
    }

  • It's interesting, but one last moment. If I want has starting point at Author, not Article... I think I can do that:

    $authors = Model\Author::query()
        ->related('articels')
        ->related('articles.author')
        ->related('articles.author.articles')
        ->where('articles.category_id', 5)
        ->get();

    and then:

    foreach ($authors as $author) {
        ...
        $articles = current($author->articles)->author->articles;
    }

    yes?
  • HarroHarro
    Accepted Answer
    Yup, you can do that.

    Just take into account that these are a lot of joins and a lot of data. Since the ORM caches all objects in memory, you may have to be careful not to select to much data at once.
  • Thank you, Harro. This task was pending for now, but I will keep in mind all you say. Thanks.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion