tags:

views:

76

answers:

2

Hello,

I have a controller/model for projects. so this controls the projects model, etc, etc. I have a homepage which is being controlled by the pages_controller. I want to show a list of projects on the homepage. Is it as easy as doing:

function index() {
    $this->set('projects', $this->Project->find('all'));        
}

I'm guessing not as I'm getting:

Undefined property: PagesController::$Project

Can someone steer me in the right direction please,

Jonesy

+4  A: 

You must load every model in the controller class by variable $uses, for example:

var $uses = array('Project');

or in action use method

$this->loadModel('Project');
Tomasz Kowalczyk
If you are satisfied with mi answer, please accept ;]
Tomasz Kowalczyk
if seems to have worked thanks! one more question though would I use it in the view like so: foreach($projects as $project){ echo $project['title']; echo '<br />'; } because it's telling me title is undefined
iamjonesy
I can't tell you this now because I don't have code to run now, but please var_dump() the content that is being returned by find() and see where exactly is 'title' located in tree.
Tomasz Kowalczyk
got it foreach($projects as $project){ echo $project['Project']['title']; echo '<br /><br />'; }
iamjonesy
A: 

For me it's more reasonable to use requestAction. This way the logic is wrapped in the controller.

In example:

//in your controller Projects:

class ProjectsController extends AppController {
  function dashboard(){
    $this->set('projects', $this->Project->find('all'));
  }
  $this->render('dashboard');
}

Bear in mind that you need to create dashboard.ctp in /app/views/projects of course.

In the Page's dashboard view (probably /app/views/pages/dashboard.ctp) add:

echo $this->requestAction(array('controller'=>'projects', 'action'=>'dashboard'));

This way the logic will remain in the project's controller. Of course you can request /projects/index, but the handling of the pagination will be more complicated.

more about requestAction(). but bear in mind that you need to use it carefully. It could slow down your application.

Nik