开发者

cakephp: how to display associated records of associated records

开发者 https://www.devze.com 2023-02-13 05:45 出处:网络
My objective is to display records for a related child model (once removed) in the view. I understand that setting the recursive value to \'1\' returns the current model, its owner(s), plus their asso

My objective is to display records for a related child model (once removed) in the view. I understand that setting the recursive value to '1' returns the current model, its owner(s), plus their associated models.

What I don't know how to do is display this in the view.

My approach has been to create a varialable in the controller holding the value of the associated model ID, but that hasn't work.

*I should also mention that I'm using a sluggable behavior for tournaments, so it's not $id that is used but $slug.

Here are the model view controller details:

Model

tournament - has many tournamentDetails

tournamentDetails - has many updates

updates - belongs to tournamentDetails

tournaments controller action = view

class TournamentsController extends AppController 

function view($slug = null) {
  if (!$slug) {
      $this->Session->setFlash(__('Invalid Tournament.', true));
      $this->redirect(array('action'=>'index'));
  }
$this->Tournament->recursive = 1;
$tournament = $this->Tournament->findBySlug($slug);
$updates = $this->Tournament->TournamentDetail->Update->find('all', array('conditions'=>array('update.tournament_detail_id' => $this->data['tournament_details'] ['id'] )) );
$this->set(compact('tournament','updates' ));

Tournament view. This is display the 'tournament details' and (ideally) related tournament detail 'updates'

<h2><?php echo $tournament['Tournament']['name']; ?></h2>

<?php foreach ($tournament['TournamentDetail'] as $tournamentDetail): ?>
 <h1><?php echo $tournamentDetail ['title']; ?></h1>
 <p><?php echo $tournamentDetail ['body']; ?></p>
<?php endforeach; ?>

<?php foreach ($updates['Update'] as $update): ?>
 <h4>Update: <?php echo $update  ['date']; ?></h4>
 <p> <?php echo $update ['Update'] ['title']; ?></p>
 <p><?php echo $update ['Update'] ['body']; ?></p>
<?php endforeach; ?>

when I debug $updates, this is all I see:

Array
(
)
开发者_运维百科

I don't know what I'm doing wrong here.

Is it not correct to construct the find conditions by using:

 ('conditions'=>array('update.tournament_detail_id' => $this->data['tournament_details'] ['id'] )

Thanks for the help. Paul


You can use the Containable Behavior to specify in detail what fields you want to obtain from associated models, associated models to associated models and so on. Then you will find all data inside the array $tournament, like in the examples in the link.

EDIT (in response the OP comment). I think that the problem may be using the magic findBy method. You would need to substitute that with the standard find, like in this example

$tournament = $this->Tournament->find('first', array(
    'conditions' => array(
        'slug' => $slug
    ),
    'contain' => array(//Put here the settings for Containable
    )
));
0

精彩评论

暂无评论...
验证码 换一张
取 消