2012-06-05 66 views
0

我试图让视图正常工作。我的模型是:注意(8):未定义索引:编号

class LocalClock extends AppModel 
{ 
    public $useDbConfig = 'default'; 
    public $useTable = 'local_clocks'; 
    //public $useTable = false; 
public function setStatType($type) 
{ 
    //$this->table = 'local_clocks'; 
    $this->_schema = array(
     'col1' => array('type' => 'int') 
     ,'col2' => array('type' => 'string') 
     ,'col3' => array('type' => 'string') 
     ,'col4' => array('type' => 'string') 
     ,'col5' => array('type' => 'string') 
     ,'col6' => array('type' => 'string') 
     ,'col7' => array('type' => 'string') 
     ,'col8' => array('type' => 'string') 
     ,'col9' => array('type' => 'string') 
     ,'col10' => array('type' => 'string') 
     ,'col11' => array('type' => 'string') 
     ,'col12' => array('type' => 'string') 
     ,'col13' => array('type' => 'string') 
     ,'col14' => array('type' => 'string') 
    ); 
} 

}

控制器是这样的:

class LocalClocksController extends AppController 
{ 
    public $scaffold = 'admin';  // allow basic view/edit for administrators 
    public $components = array('RequestHandler'); // enable checking for incoming request attributes 

    public $helpers = array('Html', 'Form'); 
public function index() 
{ 
if ($this->RequestHandler->accepts('xml')) 
{ 
    $this->LocalClock->table = 'local_clocks'; 
    $this->LocalClock->getDataSource()->tableFields['local_clocks'] = array("id", "name", "auto_offset", "utc_offset_sec", "in_month", "in_week", "in_day", "in_hour", "out_month", "out_week", "out_day", "out_hour", "offset_sec"); 
     // xml handler 
    $this->set('localClocks', $this->LocalClock->find('all')); 
    $this->set('_serialize', array('local_clocks')); 
    } 
    elseif ($this->RequestHandler->accepts('json')) 
    { 
    $this->LocalClock->getDataSource()->tableFields['local_clocks'] = array("id", "name", "auto_offset", "utc_offset_sec", "in_month", "in_week", "in_day", "in_hour", "out_month", "out_week", "out_day", "out_hour", "offset_sec"); 
     $this->set('localClocks', $this->LocalClock->find('all')); 
     $this->set('_serialize', array('local_clocks')); 
    } 
elseif($this->RequestHandler->accepts('html')) 
{ 
    $this->LocalClock->table = 'local_clocks'; 
    $this->LocalClock->getDataSource()->tableFields['local_clocks'] = array("id", "name", "auto_offset", "utc_offset_sec", "in_month", "in_week", "in_day", "in_hour", "out_month", "out_week", "out_day", "out_hour", "offset_sec"); 
     $this->set('localClocks', $this->LocalClock->find('all')); 
}  
} 

}

而且本所认为:

<p><marquee><u>Local Clocks</u></marquee></p> 

<table> 
<thead><tr> 
<th>Id</th> 
<th>Name</th> 
<th>Auto Offset</th> 
<th>UTC Offset Sec</th> 
<th>In Month</th> 
<th>In Week</th> 
<th>In Day</th> 
<th>In Hour</th> 
<th>Out Month</th> 
<th>Out Week</th> 
<th>Out Day</th> 
<th>Out Hour</th> 
<th>Offset Sec</th> 
<th>Actions</th> 
</tr></thead> 
<tbody> 

<?php 
foreach($localClocks as $LocalClock) { ?> 

<tr> 
<td><?php echo $LocalClock['id']; ?></td> 

<td><input type="button" value="View"/><input type="button" value="Edit"/><input type="button" value="Delete"/></td> 
</tr> 

<?php } ?> 

</tbody> 
</table> 
<?php debug($LocalClock); ?> 

我收到“注意(8):未定义的索引:id [AP P /查看/ LocalClocks/index.ctp 37行]”错误

这是调试的输出($ LocalClock)(从视图文件

array(
    'LocalClock' => array(
     'id' => '4', 
     'name' => 'New Test Clock', 
     'auto_offset' => false, 
     'utc_offset_sec' => '9', 
     'in_month' => '8', 
     'in_week' => '7', 
     'in_day' => '6', 
     'in_hour' => '5', 
     'out_month' => '4', 
     'out_week' => '3', 
     'out_day' => '2', 
     'out_hour' => '1', 
     'offset_sec' => '0' 
    ) 
) 

任何帮助与foreach循环的LocalClock如何正确显示id会很好。 我也试图显示其他属性,如自动偏移量,utc_offset_sec,in_month等,但为了简单起见,我将其留在视图文件之外。

另外,在顶部的模型文件中,我是否真的需要函数setStateType($ type)? 如果我确实需要这个函数,那么$ this - > _ schema = array()代码怎么样?我是否也需要这个函数。

我正在使用cakephp,而我对此很陌生。此外,我拥有的代码来自我正在开发的同一个大型项目的不同部分。

感谢

回答

2

像这样:

$LocalClock['LocalClock']['id']; 

IIRC,CakePHP的放型号名称作为第一个元素数组中,所以你需要访问LocalClock第一。

你调试表明这一点:

array(
    'LocalClock' => array(
//This One^
     'id' => '4', 
     'name' => 'New Test Clock', 
     'auto_offset' => false, 
     'utc_offset_sec' => '9', 
     'in_month' => '8', 
     'in_week' => '7', 
     'in_day' => '6', 
     'in_hour' => '5', 
     'out_month' => '4', 
     'out_week' => '3', 
     'out_day' => '2', 
     'out_hour' => '1', 
     'offset_sec' => '0' 
    ) 
) 
+0

Dude,Your awesome – RXC

1

您可以更改您的视图:

<p><marquee><u>Local Clocks</u></marquee></p> 

<table> 
<thead><tr> 
<th>Id</th> 
<th>Name</th> 
<th>Auto Offset</th> 
<th>UTC Offset Sec</th> 
<th>In Month</th> 
<th>In Week</th> 
<th>In Day</th> 
<th>In Hour</th> 
<th>Out Month</th> 
<th>Out Week</th> 
<th>Out Day</th> 
<th>Out Hour</th> 
<th>Offset Sec</th> 
<th>Actions</th> 
</tr></thead> 
<tbody> 

<?php 
foreach($localClocks as $LocalClock) { 
extract($LocalClock); 
?> 

<tr> 
    <td><?php echo $LocalClock['id']; ?></td> 

    <td><input type="button" value="View"/><input type="button" value="Edit"/><input type="button" value="Delete"/></td> 
</tr> 

<?php } ?> 

</tbody> 
</table> 
<?php debug($LocalClock); ?> 

这种方法使用的提取物,其分配由数组的键/值的变量。这不是最理想的,但会起作用。