I have a news table (1 tab.), Several pictures are attached to each news (2nd tab.), There are corresponding models.
In the controller, I get a list of news:

$news = ORM::factory('newsarchive')->find_all()->as_array(); 

You can also get pictures for each news:

 foreach ($news as $n) { $photos = $n->photos->find_all(); … 

Question : Is it necessary for me to make a separate associative array with pictures in the controller in order to display a list of news with pictures in view, or can you somehow manage (in view ) with a single news array?
If it is possible with one array, then how will it look in view ?

    2 answers 2

    Make a separate View for the output of pictures and for the output of the news and pass the view with the pictures in the view of the news as an argument:

     $photos = View::factory('photos'); $new = View::factory('new'); $new->photos = $photos; 

      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; ?> 
      • Almaz V. @. Of course I get all the news in one request. And in view , with the help of foreach I display each news separately. Just the question was as in the view when iterating through each news, to sort through / display all the photos for each of the news - Konstantin78
      • You just cited as an example photos = $ n-> photos-> find_all () ;. And this is exactly what I wrote. 20 requests for photos on 20 news. Updated the answer to your question: two nested loops. - Almaz V.
      • Almaz V. @. There, in the model, on the line public function find_all_with_photos () { error * Maximum function nesting level of '256 reached, aborting! * - what could be the problem? ... - Konstantin78
      • tried to change the values ​​in "xdebug.max_nesting_level", (the parameter was commented out by default), so the value is 256 (when the page is updated), it simply changes to the value set in " xdebug.max_nesting_level " in "php.ini" - Konstantin78