The answer to the question: not necessarily.
You can pack photos into the news array, for example, using the 'photos' key as follows:
$news = array_map(function($newsone) use ($photos){ $newsone_id = $newsone['id']; $newsone['photos'] = array_filter($photos, function($photo) use ($newsone_id) { return ($photo['news_id'] == $newsone_id); }); return $newsone; }, $news); $view->news = $news;
and in the view get like this:
echo Debug::vars( $news['photos'] );
Comment:
Confused in your question another. You will make a separate request for each news. Those. 20 news - 20 requests, this is bad in terms of performance. Plus, in the controller, it is better not to write any logic, but to write everything in the model. There and add to the conclusion of each news, photos.
I would do something like this:
in the newsarchive model
private $with_photos = FALSE; public function with_photos() { $this->with_photos = TRUE; return $this; } public function find_all_with_photos { $news = $this->find_all(); $news_ids = array_map('id', $news); $photos = ORM::factory('photos') ->where('news.id','IN', (count($news_ids)) ? $news_ids : array(0) ) ->find_all() ->as_array(); $news = array_map(function($newsone) use ($photos){ $newsone_id = $newsone['id']; $newsone['photos'] = array_filter($photos, function($photo) use ($newsone_id) { return ($photo['news_id'] == $newsone_id); }); return $newsone; }, $news); return $news; } public function find_all() { if ($this->with_photos) { return $this->find_all_with_photos(); } return $this->find_all(); } ?>
in the controller
<?php $news = ORM::factory('newsarchive') ->with_photos() ->find_all(); $view->news = $news; ?>
in the view
<?php echo Debug::vars($news['photos']); ?>
UPDATE:
output news with photos
<?php foreach ($news as $newsone): ?> <?php Debug::vars($newsone); ?> <?php foreach ($newsone['photos'] as $photo): ?> <?php Debug::vars($photo); ?> <?php endforeach; ?> <?php endforeach; ?>