Recent Posts

Tuesday, August 9, 2011

Avoid Undefined Index in CakePHP

There might be various problems regarding to undefined index in CakePHP and there are many solutions to it and some links i went through to fix the problem are:

Newby: "Undefined index" error
Something on CakePHP Notice (8): Undefined Index
CakePHP: Rendering an undefined index

However these couldn't help solve my problem, being newbie in Cake and after going through these posts, it clicked me that i might be doing something silly mistake.

Actually i was displaying news in my default page where I declared the variable news in apps_controller as below:

function beforeRender(){
$this->loadModel('News');
$todayNews = $this->News->find('first', array( 'order' => array( 'News.updated' => 'DESC' ) ) );

$this-> set('news', $todayNews);

}

And in my news controller I was using below code to render my index page:

function admin_index()
{
$allNews = $this->News->find('all',
array(
'order' => array
( 'News.updated' => 'DESC' ) )
);

$this -> set('news',$allNews);
}

You can see both had "news" being set. So when i called my index page i was always getting

Notice (8): Undefined index: News [APP\views\news\admin_index.ctp, line 18]

This made me go round and round for an hour... finally found out ti was the same variable causing problem.

then I changed the variable in my app_controller to be


function beforeRender(){
$this->loadModel('News');
$todayNews = $this->News->find('first', array( 'order' => array( 'News.updated' => 'DESC' ) ) );

$this-> set('displaynews', $todayNews);

}
and called the same in my default layout page and all worked like a charm... hope this will help save you some time that might use creatively..... Enjoy coding.

Related Posts by Categories




1 comment:

Anonymous said...

I ADAMS KEVIN, a representative Aiico Insurance plc, we trust and respect for individual differences in day out a loan. We will provide 2% of the loan's interest rate. If you are interested in this business contact us by e-mail: (adams.credi@gmail.com) now transfer their loan documents issued properly. Do you need a loan to set up business or school if you are very welcome to Aiico Insurance plc. You can also contact us by e-mail: (adams.credi@gmail.com). We first week can request a balance transfer.

DO YOU NEED LOAN FOR PERSONAL BUSINESS? IF YOU CONTACT YOUR EMAIL ABOVE TO PROCEED WITH YOUR LOAN TRANSFER IMMEDIATELY OK.

Post a Comment