cakephp如何在index.ctp中指定home和away团队名称以及模型中的关系

时间:2015-09-05 07:05:42

标签: php relationship ctp cakephp-2.7

匹配团队有多对多的关系但如果我放置关联则不起作用。以及如何从团队表中取代主队和队名,而不是 home_id team_id 。请帮忙

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 } ?>

匹配模型

class Match extends AppModel {
}

团队模型

class Team extends AppModel {
}

0 个答案:

没有答案