- To have it preserve your indentation here, use ``` alone on a line before the code (and again after, if you want to go back to unformatted text).
- I don’t think you need
if ($q->bio-> user_id == $q->id). The user object, if loaded correctly, will only include the bio for that user. - Is it intentional that you’re only checking the user’s survey if they have a bio? I thought you said they could do one or the other or both.
- Use
echo $this->element(‘thumb’, [‘bio’ => $q->bio]);, and then in your element you’ll have a variable called$biowhich is the bio entity. Or else useecho $this->element(‘thumb’, [‘q’ => $q]);and you’ll get your entire$qvariable there and can access$q->biothe same as you do in your main view.
Hello Zuluru, I tried to edit my post again and got the error that it was created too long ago and then the post was discarded. I would like to re-post but I’m still having problems preserving the indentations by using 3 backticks. When I typed in 3 backticks and then pasted in my code the indentations disappeared like before.
The elements are working now that you gave me the proper syntax so thanks for that. But my pagination no longer works. It shows the right count but all 7 of the records are displayed on the same page even though the limit was set to 4. I found the following clue in my search results: “Make sure the result of the paginate() method is assigned to a variable that is then used in the view.” However the variable that I’m using in the view is the SQL query that I created with toArray() and paginate won’t accept an array as its variable.
I didn’t need to convert the query to an array after all and I had better luck with the $query variable but $query uses the Users table and not the Bios table and the problem now is that the Users table has 13 records not 7 like the Bios table. So now the message says: “Showing 6 cooks out of 13 total” when it should say that it’s showing a total of 7 cooks.
$query = $this->fetchTable(‘Users’)->find()->contain([“Surveys”,“Bios”]);
//->toArray(); this isn’t necessary to use
$bios = $this->paginate($query);
$this->set(compact(‘bios’));
I added a WHERE clause and now everything is working!
$query = $this->fetchTable(‘Users’)->find()->contain([“Surveys”,“Bios”])->where([‘Users.id = Bios.user_id’]);
Good that it’s working, but in general if you want to show a list of bios, you’d start from the Bios table and include Users, not the other way around, like
$query = $this->fetchTable('Bios')->find()->contain(['Users' => 'Surveys']);
$bios = $this->paginate($query);
In your view, then, foreach ($bios as $bio) would give you a sequence of Bio entities, each of which would have, e.g. $bio->user and $bio->user->surveys would be the surveys completed by the user.
Alternately, you could use ->matching on the query. Your where clause may work here, but it’s not as general a syntax as matching will give you, so copy-pasting that to a new scenario may not work.
I don’t need these two lines of code any more. The first line publishes the survey only if the bio isn’t empty and the second line has been taken care of in the database query.
Hi Zuluru, thanks for the tips. I changed my code to use matching instead of where. But I didn’t change my query to first access the bios table instead of the users because I think the 2 queries are equivalent. And also I didn’t want to have to change all of the conditions so that they loop through bios instead of users.