2012-06-05 106 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); ?> 

這種方法使用的提取物,其分配由數組的鍵/值的變量。這不是最理想的,但會起作用。