The query loops over the recipes 4 times when there are 4 user_ids in the Recipe table and it loops over the recipe 1 time when there is no recipes for the user_id

public function view($id = null) {

$query = $this->fetchTable(‘Users’)->find()->matching(‘Bios’, function ($q) use ($id) {
return $q->where([‘Bios.user_id’ => $id]);
})

->contain(["Bios", "Surveys","Recipes", "Urls"])
->matching('Bios')

//  ->matching(‘Surveys’)
//   ->matching(‘Recipes’)
->all();

  $this->set(compact('query'));

}

SCREENSHOTS OF ONE USER THAT HAD NO RECIPES AND ONE USER THAT HAD 4 RECIPES

Screenshot\ from\ 2026-04-26\ 11-52-21.png
Screenshot\ from\ 2026-04-26\ 11-52-32.png

THE BOOTSTRAP CODE IS VERY CORRUPTED BUT I JUST WANT TO SEE THE OUTPUT OF THE query LOOP

<h2><?php 
foreach ($query as $q):
///you shouldn't need to do this because the join is on id = user_id

if ($id == $q->bio-> user_id):
echo $q → contributor; ?
>
<?php

if ($q → bio->home_page_image != NULL ){

echo “PICTURE HERE”;
//echo $this->element(“homepageimage”, [“bio” => $q->bio]);

?>

LINE 69 <?php

} if ($q->survey->publish == 0 && !empty($q->bio->body)) {

//echo $q->bio->body;
echo “BIO BODY”;

?>

Date This Bio Was Created: <?php $date = $q->bio->created; echo date_format($date, 'F jS, Y'); echo "

"; } elseif ($q->survey->publish == 1) { //COMMENT BACK IN LATER //echo $this->element("survey", ["survey" => $q->survey]); ?>

Date This Survey was taken: <?php $date = $q->survey->created; echo date_format($date, 'F jS, Y'); ?>

I wasn’t able to attach the screenshots for one user that had no recipes and the other user that had 4 recipes

I know I can create a second query but then I would need to use nested loops and I want to avoid that

I didn’t need nested loops after all but I needed two more queries, one for recipes and one for categories. I thought I could make the code more simple but it’s not happening