title: CakePHP 2.2.4: Undefined Index Error
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index">PHP: “Notice: Undefined variable”  and “Notice: Undefined index”</a>  </p>
  </blockquote>
  
  
  
  <p>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.</p>
  
  <p>On my USERS VIEW.CTP page I echo </p>
  
  <pre><code>$this-&gt;Custom-&gt;UserAvatar($article['User']['Avatar']['file'], $article['User']['username'], 'avatar_large');
  </code></pre>
  
  <p>and it gives me the correct avatar for the user.</p>
  
  <p>My USER MODEL is set up like so:</p>
  
  <pre><code>class User extends AppModel {
  
  var $belongsTo = array(
     'Avatar' =&gt; array(
        'className'    =&gt; 'Avatar',
        'dependent'    =&gt; true,
        'foreignKey'  =&gt; 'id = Avatar.id'    ),
  
     );
      public $hasMany = array(
          'Favorite' =&gt; array(
              'className' =&gt; 'Favorite',
          ),
          'Article' =&gt; array(
              'className'  =&gt; 'Article',
          )
      );
  
  } // end Model
  </code></pre>
  
  <p>My ARTICLE MODEL is set up like so:</p>
  
  <pre><code>class Article extends AppModel {
      public $validate = array(
          'title' =&gt; array(
              'rule' =&gt; 'notEmpty'
          ),
          'content' =&gt; array(
              'rule' =&gt; 'notEmpty'
          )
      );
  
  var $belongsTo = array(
     'User' =&gt; array(
        'className'    =&gt; 'User',
        'dependent'    =&gt; true,
        'foreignKey'  =&gt; 'user_id'),
  
     );
  
  } // end Model
  </code></pre>
  
  <p>My AVATAR MODEL is set up like so (is this model even necessary at this point?):</p>
  
  <pre><code>class Avatar extends AppModel {
  
  var $hasmany = array(
     'User' =&gt; array(
        'className'    =&gt; 'User')
     );
  
  } // end Model
  </code></pre>
  
  <p>I put $users = array('Users', 'Avatar') in the ARTICLES CONTROLLER in case that did anything.</p>
  
  <p>I'm not sure if the relationships are correct.
  My tables:</p>
  
  <pre><code>Articles (id, user_id...etc) 
  Users(id, avatar_id...etc)
  Avatars(id, file...etc)
  </code></pre>
  
  <p><strong>EDIT:</strong> The exact error reads...</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>EDIT:</strong> ArticlesController.php as requested</p>
  
  <pre><code>App::uses('AppController', 'Controller');
  /**
   * Articles Controller
   *
   * @property Article $Article
   */
  class ArticlesController extends AppController {
  
  
  /**
   * index method
   *
   * @return void
   */
      public function index() {
          // $this-&gt;Article-&gt;recursive = 0; // I used this originally
          // $this-&gt;set('articles', $this-&gt;paginate()); // I used this originally
                  $this-&gt;Article-&gt;find('all');
          $this-&gt;set('articles', $this-&gt;paginate());
  
      }
  </code></pre>
  
  <p>The debug of $articles or $article stops at User. Avatar should be an array within the User array.</p>
  
  <pre><code>$article = array(
      'Article' =&gt; array(
          'id' =&gt; '11',
          'page_id' =&gt; '2',
          'topic_id' =&gt; '7',
          'user_id' =&gt; '29',
          'title' =&gt; 'Test title for article 11'
      ),
      'Page' =&gt; array(
          'id' =&gt; '2',
          'category_id' =&gt; '1',
          'title' =&gt; 'Dexter',
      ),
      'Topic' =&gt; array(
          'id' =&gt; '7',
          'page_id' =&gt; '11',
          'title' =&gt; 'Press'
      ),
      'User' =&gt; array(
          'password' =&gt; '*****',
          'id' =&gt; '29',
          'username' =&gt; 'testuser',
          'avatar_id' =&gt; '519'
      )
  )
  </code></pre>