【发布时间】:2013-01-07 01:01:12
【问题描述】:
我的想法是这样的。我在 Model 中创建了一个这样的函数。
public function status($id = null)
{
$conditions = array('Transaction.catalogue_id' => $id);
$status = $this->find('first',array('conditions' => $conditions, 'fields' => array('Transaction.status'),'order' => array('Transaction.id' => 'desc'))
);
return $status['Transaction']['status'];
}
我如何从这个循环中发送 $id 的值?
<table cellspacing="0" cellpadding="0" border="0" class="all">
<thead>
<tr>
<th>No</th>
<th>Title</th>
<th>Author</th>
<th>ISBN</th>
<th>Location</th>
<th>Availability</th>
</tr>
</thead>
<?php $count = 1;?>
<tbody>
<?php foreach ($Catalogue as $Catalogue): ?>
<tr>
<td><?php echo $count++; ?></td>
<td><?php echo $this->Html->link($Catalogue['Catalogue']['title'],array('controller' => 'transactions', 'action' => 'view',$Catalogue['Catalogue']['id']),array('escape' => false, 'class' => 'ajax')); ?></td>
<td><?php echo $Catalogue['Catalogue']['author']; ?></td>
<td><?php echo $Catalogue['Catalogue']['isbn']; ?></td>
<td><?php echo $Catalogue['Location']['rack']; ?></td>
<td><?php echo $this->getstatus($Catalogue['Catalogue']['id'])</td>
</tr>
<?php endforeach; ?>
</tbody>
<?php unset($Catalogue); ?>
</table>
我们可以 $this->getstatus($Catalogue['Catalogue']['id']) 吗?
【问题讨论】:
-
PS:这不是很好的编码。您不检查返回值是否实际填充。如果 id 无效,您将触发未定义的索引错误...应该添加
if (!$status) { return false; }等。
标签: cakephp user-defined-functions cakephp-2.2