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 />
单击“完成任务”将返回一个空列表(此列表中没有任务)。谁能看到链接上有什么问题?提前谢谢!
答案 0 :(得分:1)
您正在使用变量名称 $ Tasks 混合大小写。在您的示例中,您将阵列分配给 $ tasks ,但是在您的视图中,您正在尝试检查变量名称 $ Tasks ,这将变为空,因为它从来没有正确设置。
更改此行:
$this->set('tasks', $tasks);
要强>
$this->set('Tasks', $tasks);
答案 1 :(得分:0)
正如McWayWeb上面提到的变量的情况是不正确的。
当您在视图中设置小写变量时,我会在那里进行更改。将视图中任务的所有计数更改为任务。
我还会考虑使用下面的
同时设置所有viewVars$this->set(compact('tasks','status));