我正在尝试使视图正常工作。我的模型是:
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): Undefined index: id [APP/View/LocalClocks/index.ctp, line 37]”错误
这是 debug($LocalClock) 的输出(视图文件中 foreach 循环的 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'
)
)
有关如何正确显示 id 的任何帮助都会很棒。我还尝试显示其他属性,如自动偏移、utc_offset_sec、in_month 等,但为简单起见,我将这些属性排除在视图文件之外。
另外,在顶部的模型文件中,我真的需要函数 setStateType($type) 吗?如果我确实需要这个函数,那么 $this->_schema = array() 代码呢,我是否也需要它。
我正在使用 cakephp,我对此并不陌生。此外,我拥有的代码来自我正在从事的同一个大项目的不同部分。
谢谢