0

Match and team has many to many relationship but it does not work if i put the associations. and how to get home and away team names from the team table instead of home_id and team_id. please help

MatchesController.php

class MatchesController extends AppController
{
    public $uses = array('Match','Team');
    public function index()
    {
        $matches=$this->Match->find("all");
        $this->set("matches",$matches);
    }

    public function add() 
    {
        if ($this->request->is('post')) 
        {
            $this->Match->create();
            if ($this->Match->save($this->request->data)) 
            {
                $this->Flash->success(__('Match Has Been Added'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Flash->error(__('Something Went Wrong.'));
        }
        $teams = $this->Team->find('list');
        $this->set('teams',$teams);
    }

index.ctp

<?php foreach($matches as $match) { ?>
    <tr>
        <td><?php echo $match['Match']['id']; ?></td>
        <td><?php echo $match['Match']['home_id']; ?></td>
        <td><?php echo $match['Match']['away_id']; ?></td>
        <td><?php echo $match['Match']['date']; ?></td>
        <!--    <td><?php// echo $match['Match']['time']; ?></td> -->
        <td><?php echo $match['Match']['location']; ?></td>
        <!--    <td><?php //echo $match['Match']['winner_id']; ?></td> -->
        <!--    <td><?php //echo $match['Match']['winner_id']; ?></td> -->
        <!--        <td><?php //echo $match['Match']['home_goals']; ?></td> -->
        <!--    <td><?php //echo $match['Match']['away_goals']; ?></td>-->
        <!--    <td><?php //echo $match['Match']['type']; ?></td> -->
        <td>
        <?php
        echo $this->Html->link('View', array('action'=>'view',$match['Match']['id']), array('class'=>'btn btn-primary'));
        echo $this->Html->link('Edit', array('action'=>'edit',$match['Match']['id']), array('class'=>'btn btn-success'));
        echo $this->Form->postLink('Delete', array('action'=>'delete',$match['Match']['id']),array('confirm'=>'Are You sure you want to delete Student', 'class'=>'btn btn-danger'));?>
        </td>
    </tr>
<?php } ?>

Match model

class Match extends AppModel {
}

Team Model

class Team extends AppModel {
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • improved formatting, Added Tags, – Mohit S Sep 07 '15 at 08:57
  • `` - are you asking for someone to write your template file for you? See the docs regarding [contain](http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#eager-loading-associations) - that is how you pull association record data into a result set. – AD7six Sep 07 '15 at 09:00

0 Answers0