-1

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'
    )
)
Community
  • 1
  • 1
bowlerae
  • 924
  • 1
  • 14
  • 37
  • What is the error?Please print it in full – techie_28 Dec 03 '12 at 18:11
  • 2
    expand the Notice. did you debug the array to see if the data is even in there? that is usually it. – Colby Guyer Dec 03 '12 at 18:27
  • 1
    This is simple array error likely having nothing to do with your models (though Colby is correct, your foreignKey is very wrong, and should be like he has it). Just echo/debug the array and fix your file accordingly. – Dave Dec 03 '12 at 18:33

2 Answers2

0

It looks like your User to Avatar belongsTo is not setup right but you say it is working? In fact, you should not need the foreignKey field if you are using convention. So unless there is something else going on, your

'foreignKey'  => 'id = Avatar.id'

looks like it should be

'foreignKey'  => 'avatar_id'

You really need to include more code/full error for us to determine what is going on.

Colby Guyer
  • 594
  • 3
  • 14
  • I get the same result either way. Avatar outputs correctly on users view.ctp but now articles index.ctp – bowlerae Dec 03 '12 at 18:28
  • debug $article right before that is called and make sure $article['User']['Avatar']['file'] and $article['User']['username'] have values – Colby Guyer Dec 03 '12 at 18:29
  • I'm not exactly sure how I do that. Cookbook says to debug with "Debugger::dump($foo);" I replaced foo with article (and articles). Where do I place it or do I use a different debugger? I tried placing in both controller and model. – bowlerae Dec 03 '12 at 18:41
  • make sure debug is turned on in your core file (value of 1 would do it) and then do some simple debugging. http://book.cakephp.org/2.0/en/development/debugging.html something like debug($article['User']['Avatar']['file']); should work – Colby Guyer Dec 03 '12 at 18:47
  • ok the debug array shows the correct information for the article and the associated user but does not show the avatar information. This what I don't understand. The avatar displays in the users view. I have changed my user and article models but nothing so far has made it work. – bowlerae Dec 03 '12 at 18:55
  • you would have to show us controller code that does the find. – Colby Guyer Dec 03 '12 at 19:35
0

Setting recurisve = 2 in article controller did the trick. Also had to adjust my association between users and avatars in the user model (had to have both user belongsto avatar and user has many avatars).

I'm a little worried recursize = 2 will slow down the application since article is associated with comments which is associated with all of the other user content modules I have on my site.

bowlerae
  • 924
  • 1
  • 14
  • 37