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...
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.
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.