简体   繁体   中英

CakePHP 3: How to display associated data for each record in index.ctp?

I have two tables artists and pictures with a artist hasMany paintings relation.

Instead of having a separate view.ctp for every artist that displays the related pictures.I just want to display everything in the index.ctp file all artists listed with each ones associated pictures (list of IDs).

After reading the cookbook and a bazillion threads I'm totally confused and don't understand how I have to do that. (I'm still very new to PHP and Cakephp)

In the view( ) function for the single artist I can get the related pictures easily like this:

Controller : ArtistsController.php

..
public function view($id=null)
{
   $artist = $this->Artists->get($id, [
      'contain' => ['Paintings']
   ])
...
}

And then get everything in the corresponding view.ctp by doing:

view.ctp
<?php foreach ($artist->paintings as $paintings): ?>
<ul><li> <?= h($paintings->id) ?> </li></ul>
....
<?php endforeach; ?>

But how do I go about doing something similar for the index.ctp / index() function for each artist listed in the table? So I can essentially have sth like this:

<?php foreach ($artists as $artist): ?>
    <tr><td> <?= h($artist->name) ?> </td></tr>
    <tr><td> <?php foreach ($artist->paintings as $paintings): ?>
             <ul><li> <?= h($painting->id) ?> </li></ul>
             <?php endforeach; ?>
    </tr></td>
<?php endforeach; ?>

I know it's not really working like that but how would I go about doing it? Do I have to use a find() query in the Controllers index() function and store the results before retrieving it in the view?! I did try that but it either displayed nothing or it displayed an SQL query itself because I messed up the syntax somehow...

So after trying various things from the book and searching for hours it seems I am missing some basic understanding in my brain here. Also all similar questions I found lead me to nothing.

Even a hint where to start with this would be great! Thanks a lot!

OK It may I understood, I am giving you an example it may help you

There is two table 1) artists 2) pictures .

Artists has 2 fields id,name
And pictures has 3 fields id,artist_id,picture // here artist_id has made a relation between artists and pictures.

Here Artists has many relation with pictures, so we will write in Model\\Table\\ArtistsTable.php

 public function initialize(array $config)
 {
        parent::initialize($config);

        $this->table('artists');
        $this->displayField('name');
        $this->primaryKey('id');

        $this->hasMany('Pictures', [
            'foreignKey' => 'artist_id'
        ]);
 }

Then Controller\\ArtistsController.php index method should like below code

public function index()
{
        $this->paginate =[
            'contain'   => ['Pictures']   //with artists We also fetch pictures
        ];
        $this->set('artists', $this->paginate($this->Artists));
        $this->set('_serialize', ['artists']);
}

Now in Template\\Artists\\index.ctp you can fetch data like below code

<?php foreach ($artists as $artist): ?>
    <?= h($artist->name) ?>  // fetch artists name from artists table 
  <?php foreach ($artist->pictures as $pictures): ?>  
    <?= h($pictures->picture) ?>  // fetch pictures according to artists 
  <?php endforeach; ?>
<?php endforeach; ?>

Now you get the output like

jondu 1 2 3

Here jondu is a name and he has 3 pictures 1,2,3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM