I have loaded a query in a pagination and how do i display the data in a view in cakephp3?
In cakephp2 this was dead easy. In the docs I cant find how to display data from associated tables ?
None of this works but the data exists
<?php foreach ($lessons as $bookmark): ?>
<tr>
<?php
// debug( $bookmark);
?>
<td><?php echo $bookmark['Lesson']['id'] ?></td>
<td><?= h($bookmark->Lessons->id) ?></td>
<td><?= h($bookmark->lesson_date) ?></td>
<td><?= h($bookmark->Tutor.first_name) ?></td>
<td><?= h($bookmark->Tutor.last_name) ?></td>
<td><?= h($bookmark->Subject.name) ?></td>
</tr>
<?php endforeach; ?>
Hi your code is wrong you must code exaclly like $bookmark->Tutor->first_name
.
Still isnt working and i debugged the data on the view below.
This was so easy in cakephp2
Notice (8): Trying to get property of non-object
<?= h($bookmark->Lessons->id) ?>
<?= h($bookmark->Lesson->lesson_date) ?>
<?= h($bookmark->Tutor->first_name) ?>
<?= h($bookmark->Tutor->last_name) ?>
<?= h($bookmark->Subject->name) ?>
object(App\Model\Entity\Lesson) {
'tutoring_type_id' => (int) 1,
'students' => [
'id' => '75',
'first_name' => 'Jes',
'last_name' => 'ett'
],
'lessons' => [
'id' => '5399',
'lesson_date' => '2015-07-09'
],
'tutors' => [
'id' => '12',
'first_name' => 'Andrew',
'last_name' => 'Toy'
],
'subjects' => [
'id' => '16',
'name' => 'Maths: Year 7 - 10'
],
jagguy:
tutors
you are mistaking.you must convert above code to tutors.just this
<?= h($bookmark->tutors->first_name) ?> didnt work
where is this in the docs as I only see a basic example only?
yes there are a lot of example.I don’t why you have problem that is very simple.
SO what do i do as i got rid of the pagination to make it simpler
//controller
$this->set(compact('query3'));
$this->set('_serialize', ['query3']);
//view
<?php
foreach ($query3 as $bookmark):
?>
<tr>
<?php
// debug( $bookmark);
?>
<td><?= h($bookmark->lessons->id) ?></td>
<td><?= h($bookmark->lessons->lesson_date) ?></td>
<td><?= h($bookmark->tutors->first_name) ?></td>
<td><?= h($bookmark->tutors->last_name) ?></td>
</tr>
<?php endforeach; ?>
jagguy
June 9, 2016, 11:47am
8
I can solve the problem but I dont get why I need to have model names in the select with lower case names and I dont get why these are not objects
<td><?= $bookmark->lessons['id'] ?></td>
<td><?= $bookmark->lessons['lesson_date'] ?></td>
<td><?= $bookmark->tutors['first_name'] ?></td>
<td><?= $bookmark->tutors['last_name'] ?></td>
<td><?= $bookmark->subjects['name'] ?></td>
<td><?= $bookmark->TutoringTypes['value'] ?></td>
neha02
October 17, 2019, 6:25am
9
jagguy:
subject
well i guess, your Lesson variable includes these arrays and the names of these arrays are all in lower case. and you are trying to fetch data from those arrays so you have to give exact same name as it is given in that lesson array’s data… try analyzing your debug code once more you’ll then get to knw what m talking about.