以下链接用于Favors列表!它链接到用户所在的地方,但正在帮助列表中使用。因此,有利于变量。
我有三个模型Users,Places和Favors。用户有很多好处和一个地方,一个好处属于用户。
<?php foreach($favours as $favour): ?>
<p><?php echo $this->Html->link($favour['User']['firstname'] . ' ' . $favour['User']['lastname'], array('controller'=>'users','action'=>'view','userName'=>$favour['User']['username'])); ?> in <?php echo $this->Html->link($favour['Place']['name'], array('controller'=>'places','action'=>'view',$favour['Place']['id'])); ?> asked a favour <?php echo $favour['Favour']['datetime']; ?></p>
<h3><?php echo $this->Html->link($favour['Favour']['title'], array('controller'=>'favours','action'=>'view',$favour['Favour']['id'])); ?></h3>
如果我收到错误消息,说明Place未定义,我该如何显示链接。
这是该列表的控制器操作:
function index()
{
$favours = $this->paginate();
if (isset($this->params['requested']))
{
return $favours;
}
else
{
$this->set('favours', $favours);
}
}
答案 0 :(得分:0)
确保在为Favor获取数据时包含Place。
答案 1 :(得分:0)
这是你在favours_controller中的样子:
function index(){
$favours = $this->Favour->find('all');
$places = $this->Favour->Place->find('all');
$this->paginate();
$this->set(compact('users', 'places');
}
这是它在index.ctp中的样子:
<?php foreach($favours as $favour): ?>
<?php echo $this->Html->link($favour['Favour']['username'], array('controller'=>'users','action'=>'view', $favour['Favour']['username'])); ?>
<?php endforeach; ?>
<?php foreach($places as $place): ?>
<?php echo $this->Html->link($place['Place']['place'], array('controller'=>'places','action'=>'view', $place['Place']['place'])); ?>
<?php endforeach; ?>
您可能还需要定义uses
变量:
var $uses = array('Place');