我收到错误Illegal string offset ..试图在Stackoverflow上搜索,但他们没有帮助我.. 请帮助
function Fetch(){ 返回$ this-> find(' list',array(' fields' => array(' Employee.id', ' Employee.firstname'' Employee.lastname'' Employee.salary') )); }
public function index(){ $这 - >设置('员工',$这 - >员工 - >取());
}
<?php
$id = 0;
foreach($employees as $e):?>
<? $id++ ?>
<tr>
<td><?php echo $e{'Employee'}{'id'} ?></td>
<td><?php echo $e['Employee']['firstname'], $e['Employee']['lastname'] ?></td>
<td>2014-04-24</td>
<td>2014-04-29</td>
<td style="text-align:center"><?php echo $e['Employee']['salary'] ?></td>'
</tr>
<?php endforeach; ?>
Warning (2): Illegal string offset 'Employee' [APP\View\Employees\index.ctp, line 19]
Warning (2): Illegal string offset 'id' [APP\View\Employees\index.ctp, line 19]
I
Warning (2): Illegal string offset 'Employee' [APP\View\Employees\index.ctp, line 20]
Warning (2): Illegal string offset 'firstname' [APP\View\Employees\index.ctp, line 20]
I
Warning (2): Illegal string offset 'Employee' [APP\View\Employees\index.ctp, line 20]
Warning (2): Illegal string offset 'lastname' [APP\View\Employees\index.ctp, line 20]
Warning (2): Illegal string offset 'Employee' [APP\View\Employees\index.ctp, line 23]
Warning (2): Illegal string offset 'salary' [APP\View\Employees\index.ctp, line 23]
答案 0 :(得分:1)
使用find('all')
。当您只需要检索2个字段时,find('list')
更合适
function Fetch() {
return $this->find('all', array(
'fields' => array('Employee.id', 'Employee.firstname','Employee.lastname','Employee.salary') )
);
}
然后使用$e
代替$e['Employee']['id']
代替$e{'Employee'}{'id'}
。
如果您仍然收到错误,请在视图中debug($e)
查看其格式是否正确