Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
Many people have asked questions regarding the "Notice (8) Undefined Index" error message but none of the solutions are working for me. I'm not sure if my model relationships are set up correctly. Please advise. I'm getting the undefined index error on the ARTICLES INDEX.CTP page and it says that "Avatar" is the undefined part.
On my USERS VIEW.CTP page I echo
$this->Custom->UserAvatar($article['User']['Avatar']['file'], $article['User']['username'], 'avatar_large');
and it gives me the correct avatar for the user.
My USER MODEL is set up like so:
class User extends AppModel {
var $belongsTo = array(
'Avatar' => array(
'className' => 'Avatar',
'dependent' => true,
'foreignKey' => 'id = Avatar.id' ),
);
public $hasMany = array(
'Favorite' => array(
'className' => 'Favorite',
),
'Article' => array(
'className' => 'Article',
)
);
} // end Model
My ARTICLE MODEL is set up like so:
class Article extends AppModel {
public $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'content' => array(
'rule' => 'notEmpty'
)
);
var $belongsTo = array(
'User' => array(
'className' => 'User',
'dependent' => true,
'foreignKey' => 'user_id'),
);
} // end Model
My AVATAR MODEL is set up like so (is this model even necessary at this point?):
class Avatar extends AppModel {
var $hasmany = array(
'User' => array(
'className' => 'User')
);
} // end Model
I put $users = array('Users', 'Avatar') in the ARTICLES CONTROLLER in case that did anything.
I'm not sure if the relationships are correct. My tables:
Articles (id, user_id...etc)
Users(id, avatar_id...etc)
Avatars(id, file...etc)
EDIT: The exact error reads...
Notice (8): Undefined index: Avatar [/APP/View/Articles/index.ctp, line 35]
include- APP/View/Articles/index.ctp, line 35
View::_evaluate() - CORE/Cake/View/View.php, line 920
View::_render() - CORE/Cake/View/View.php, line 883
View::render() - CORE/Cake/View/View.php, line 475
Controller::render() - CORE/Cake/Controller/Controller.php, line 957
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 193
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 161
[main] - APP/webroot/index.php, line 92
EDIT: ArticlesController.php as requested
App::uses('AppController', 'Controller');
/**
* Articles Controller
*
* @property Article $Article
*/
class ArticlesController extends AppController {
/**
* index method
*
* @return void
*/
public function index() {
// $this->Article->recursive = 0; // I used this originally
// $this->set('articles', $this->paginate()); // I used this originally
$this->Article->find('all');
$this->set('articles', $this->paginate());
}
The debug of $articles or $article stops at User. Avatar should be an array within the User array.
$article = array(
'Article' => array(
'id' => '11',
'page_id' => '2',
'topic_id' => '7',
'user_id' => '29',
'title' => 'Test title for article 11'
),
'Page' => array(
'id' => '2',
'category_id' => '1',
'title' => 'Dexter',
),
'Topic' => array(
'id' => '7',
'page_id' => '11',
'title' => 'Press'
),
'User' => array(
'password' => '*****',
'id' => '29',
'username' => 'testuser',
'avatar_id' => '519'
)
)