CakePHP非法字符串偏移量

时间:2014-04-25 12:06:43

标签: cakephp

我收到错误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]

1 个答案:

答案 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)查看其格式是否正确