【发布时间】:2015-05-14 19:48:42
【问题描述】:
CakePhp 中的总 n00b
我试图链接到从控制器中定义的变量 $status 检索数据的视图,给定简单的条件“完成”或“待定”。
给定模型:
<?php
class Task extends AppModel {
var $name = 'Task';
var $validate = array(
'title' => array(
'rule' => 'notEmpty',
'message' => 'Title of a task cannot be empty'
)
);
}?>
任务控制器:
function index($status=null) {
if($status == 'done')
$tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '1')));
else if($status == 'pending')
$tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '0')));
else
$tasks = $this->set('Tasks', $this->Task->find('all'));
$this->set('tasks', $tasks);
$this->set('status', $status);
}
最后是视图:
<h2>Tasks</h2>
<?php if(empty($Tasks)): ?>
There are no tasks in this list
<?php else: ?>
<table>
<tr>
<th>Title</th>
<th>Status</th>
<th>Created</th>
<th>Modified</th>
<th>Actions</th>
</tr>
<?php foreach ($Tasks as $Task): ?>
<tr>
<td>
<?php echo $Task['Task']['title'] ?>
</td>
<td>
<?php
if($Task['Task']['done']) echo "Done";
else echo "Pending";
?>
</td>
<td>
<?php echo $Task['Task']['created'] ?>
</td>
<td>
<?php echo $Task['Task']['modified'] ?>
</td>
<td>
<?php echo $this->Html->link('Edit', array('action'=>'edit', $Task['Task']['id'])); ?>
<?php echo $this->Html->link('Delete', array('action'=>'delete', $Task['Task']['id']), null, 'Are you sure you want to delete this task?'); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php echo $this->Html->link('List Done Tasks', array('action'=>'index','done')); ?><br />
<?php echo $this->Html->link('List Pending Tasks', array('action'=>'index', 'pending')); ?><br />
单击“完成任务”会返回一个空列表(此列表中没有任务)。任何人都可以看到链接上有什么问题吗?提前致谢!
【问题讨论】:
-
它应该返回什么?当你直接设置 $tasks 变量时设置了什么:
$this->set('tasks', $this->Task->find('all', array('conditions' => array('Task.done' => 1))));如果你将数组的内容转储到 $tasks 中,它会给你正确的结果吗? -
是的。它返回内容(字段:完成/待处理)。该查询还检索正确的数据(5 个条目“完成”,4 个条目“待定”)。我只是试图将“完成”条目(视图中的链接 1)和“待处理”条目(链接 2)单独传递给视图。像这样:
标签: cakephp