Indzi
Indzi

Reputation: 390

CakePHP Illegal string offset

I am getting the error Illegal string offset.. tried to search on Stackoverflow but they are not helping me.. Please Help

My Model

function Fetch() { return $this->find('list', array('fields' => array('Employee.id', 'Employee.firstname','Employee.lastname','Employee.salary') )); }

My Controller

public function index(){ $this->set('employees',$this->Employee->Fetch());

}

My View

  <?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; ?>

My Error

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]

Upvotes: 0

Views: 4781

Answers (1)

cornelb
cornelb

Reputation: 6066

Use find('all'). find('list') is more suitable when you need only 2 fields retrieved

function Fetch() { 
    return $this->find('all', array(
        'fields' => array('Employee.id', 'Employee.firstname','Employee.lastname','Employee.salary') )
    ); 
}

Then use $e like $e['Employee']['id'] instead of $e{'Employee'}{'id'}.

If you are still getting errors, debug($e) in the view to see if it's in the correct format

Upvotes: 1

Related Questions