# 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

**URL:** <https://discourse.cakephp.org/t/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/12836>\
**Category:** Need Help\
**Created:** [April 26, 2026, 4:10pm UTC](https://discourse.cakephp.org/t/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/12836 "2026-04-26T16:10:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [April 26, 2026, 4:10pm UTC](https://discourse.cakephp.org/t/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/12836/1 "2026-04-26T16:10:25Z")

</div>

```auto
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; ?

```

```auto
>

```

\<?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'); ?\>

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [April 26, 2026, 4:13pm UTC](https://discourse.cakephp.org/t/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/12836/2 "2026-04-26T16:13:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [April 26, 2026, 6:07pm UTC](https://discourse.cakephp.org/t/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/12836/3 "2026-04-26T18:07:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [April 26, 2026, 7:57pm UTC](https://discourse.cakephp.org/t/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/12836/4 "2026-04-26T19:57:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [April 27, 2026, 9:58pm UTC](https://discourse.cakephp.org/t/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/12836/5 "2026-04-27T21:58:57Z")

</div>

I think I found the solution. I will post it soon.

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [April 27, 2026, 10:21pm UTC](https://discourse.cakephp.org/t/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/12836/6 "2026-04-27T22:21:29Z")

</div>

This is what I found  
// In your Controller  
$query = $this-\>Table-\>find()  
-\>contain([  
‘HasOneModel’, // One-to-one  
‘HasManyModel’ // One-to-many  
])  
-\>where([‘Table.id’ =\> $id]);

$records = $this-\>paginate($query);

// In your view (or controller)  
foreach ($records as $record) {  
// 1. Primary data  
echo $record-\>field;

```auto
// 2. Accessing hasOne (single object)
echo $record->has_one_model->field;

// 3. Looping through hasMany (collection)
foreach ($record->has_many_model as $related) {
    echo $related->field;
}

```

}

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [April 29, 2026, 11:32pm UTC](https://discourse.cakephp.org/t/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/12836/7 "2026-04-29T23:32:21Z")

</div>

My code was already working but my intention was to make it cleaner. Therefore I chose to use a sql query with a contain clause for 4 tables. I used the matching clause which meant I had to explicitly ask for a left join and I still couldn’t access the 2 has\_many tables in my view. I also had a fifth table that I couldn’t put in my sql query because it had no foreign key so I had to make a separate query for that. I only got the 2 has\_one tables to work in my view. I ended up with 4 foreach loops and I couldn’t read my code any more and it looped several times even though I had a where condition that set the query string to the primary key. I couldn’t see where the if, ifelses, and elses ended because I wasn’t using an editor that showed the end tags. I was trying to simplify my code but it just got more complicated and became unreadable. I think I will just restore my original code now and abort this unsuccessful exercise. I’m sorry I didn’t format the code that I pasted in previously. I didn’t think seeing the code clearly was important at that point. If anyone thinks I should revive my code and try again let me know. I just can’t see right now that it’s worth it so goodbye for now.
