Search code examples
cakephppaginationelementpaginator

Paginator errors while using elements


I have news with comments. I'd like to paginate comments but I have the errors like this:

Notice (8): Undefined index: count [CORE\Cake\View\Helper\PaginatorHelper.php, line 646]

and of course I don't see numbers, links etc.

Do you have any idea how to solve this problem? Thanks!

Component:

public function viewFromNewsId($news_id = null) {
$this->NewsComment->recursive = 0;
$this->Paginator->settings = array('conditions' => array('NewsComment.news_id' => $news_id, 'NewsComment.is_active' => '1'), 'limit' => 5, 'order' => array('NewsComment.id' => 'desc'));
$newsComments = $this->Paginator->paginate('NewsComment');
if (isset($this->params['requested'])){
return $newsComments;
}
}

Element:

$newsIdFromUrl = $this->params['pass'][0];
$newsComments = $this->requestAction("newsComments/viewFromNewsId/$newsIdFromUrl");
foreach($newsComments as $newsComment):
$this->App->showNewsComment($newsComment);
endforeach;
echo $this->Paginator->counter('Liczba newsów: {:count} | ');
echo $this->Paginator->prev('<< wstecz', null, null, array('class' => 'disabledText'));
echo $this->Paginator->numbers(array('separator' => ' ', 'before' => ' | ', 'after' => ' | ', 'modulus' => '10'));
echo $this->Paginator->next('dalej >>', null, null, array('class' => 'disabledText'));
echo "<br />";

View: echo $this->element('newsViewComments');


Solution

  • I found answer by myself ;)

    Check this out - maybe someone of you will use this. I know there is a lot of people with the same problem.

    Answer: Just don't use elements, edit view function in your controller like this:

    public function view($id = null) {
    $this->Paginator->settings = array('conditions' => array('NewsComment.news_id' => $id), 'limit' => 5, 'order' => array('NewsComment.id' => 'desc'));
    $newsComments = $this->Paginator->paginate('NewsComment');
    
    
    $options = array('conditions' => array('News.' . $this->News->primaryKey => $id));
    $news = $this->News->find('first', $options);
    $this->set(compact('news', 'newsComments'));
    }
    

    How, you have two variables in your view.ctp: first one $news - with news data like title and text and second one $newsComments to use with foreach and pagination

    But, remember about correct association in models.