cakephp:notice 8:undefined index:id error

时间:2011-09-26 09:24:28

标签: cakephp notice undefined-index

我收到未定义索引:id [APP \ controllers \ merry_parents_controller.php,第31行]代码

debug($this->data['MerryParent']);
    echo 'id: '.$this->data['MerryParent']['id'];

 MerryParentsController::login() - APP\controllers\merry_parents_controller.php, line 31
 Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 204
 Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 171
 [main] - APP\webroot\index.php, line 83

我的代码:

merry_parents_controller.php

if ($this->Auth->user()){
            //debug($this->Auth->user());
            $this->data['MerryParent']=$this->Auth->user();
            debug($this->data['MerryParent']);
            echo 'id: '.$this->data['MerryParent']['id'];

for debug($ this-> data ['MerryParent']),记录显示正常:

Array
 (
  [MerryParent] => Array
    (
    [id] => 2
    [initial] => Ms
    [username] => hong
    [email] => hong7@yahoo.com
    [password2] => hong7
    [landline] => 
    [mobile] => 293472389472
    [address] => dsaflkjasdlfk
    [state_id] => 5
    [city_id] => 62
    [postal_code] => 825729
    [created] => 2011-08-02 10:29:59
    [modified] => 2011-09-26 06:42:27
     )
)

但是当我尝试的时候 echo'id:'。$ this-> data ['MerryParent'] ['id'];

我收到通知8:未定义的索引错误。我不知道问题是什么。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

您的数组如下所示:

Array
(
    [MerryParent] => Array
    (
        [MerryParent] => Array
        (
            [id] => 2
            ...

您正在寻找的ID位于$this->data['MerryParent']['MerryParent']['id']。你可能想要

$this->data = $this->Auth->user();