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
I think I found the solution. I will post it soon.
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;
// 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;
}
}
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.