Vizualni
Vizualni

Reputation: 381

How to create navigation in cakephp using only models?

Title says all. I'll have navigation that looks like this and it's dynamically created from database.

<ul>
    <li><a href='controller/view/1'>1</li>
    <li><a href='controller/view/2'>2</li>
    <li><a href='controller/view/3'>3</li>
</ul>

It should be same on every page (by page, I mean on every controller), except for the active one. It's my first time in cakephp.

This is my elements file:

<ul id="left-navigation">
<?php
App::import('Model','Godina');
$navigacija = $this->Godina->find('all');
foreach($navigacija as $nav){
echo '<li>' . $this->html->link($nav['Godina']      ['godina'],array('controller'=>'godina','action'=>'program',$nav['Godina']['godina'])
) . '</li>';
}

?>

And this is the error that I am getting.

Fatal error: Call to a member function find() on a non-object in     /Applications/MAMP/htdocs/cakephp/app/View/Elements/godineNavigacija.ctp on line 4

And since my knowledge of cakephp core is quite low, it just doesn't work. Thanks.

Upvotes: 0

Views: 200

Answers (1)

deceze
deceze

Reputation: 522016

App::import just loads the file, like PHP's include, it does not instantiate an object. So $this->Godina does not exist. You can do this:

$Godina = ClassRegistry::init('Godina');
$Godina->find(...);

For proper MVC separation, you should not use models in the view though. You can fetch and set the data in AppController::beforeFilter, so it does it on every page, then output in the view. Alternatively, look into requestAction.

Upvotes: 2

Related Questions