Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: using models in different controllers

Tags:

php

cakephp

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

like image 278
iamjonesy Avatar asked Sep 12 '10 21:09

iamjonesy


People also ask

How do I find my controller name in cakephp?

Use $this->params['controller'] to get the current controller. You can do a debug($this->params) to see other available variables. Save this answer.

What is loadModel cakephp?

The loadModel() function comes handy when you need to use a model table/collection that is not the controller's default one: // In a controller method. $ this->loadModel('Articles'); $recentArticles = $this->Articles->find('all', [ 'limit' => 5, 'order' => 'Articles.created DESC' ]);

How can I get data from database in cakephp?

To view records of database, we first need to get hold of a table using the TableRegistry class. We can fetch the instance out of registry using get() method. The get() method will take the name of the database table as argument. Now, this new instance is used to find records from database using find() method.


2 Answers

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');
like image 121
Tomasz Kowalczyk Avatar answered Sep 21 '22 05:09

Tomasz Kowalczyk


In my opinion the proper way to do this is add a function to your current model which instantiates the other model and returns the needed data.

Here's an example which returns data from the Project model in a model called Example and calls the data in the Example controller:

Using Project Model inside Example Model:

<?php
/* Example Model */
App::uses('Project', 'Model');
class Example extends AppModel {

    public function allProjects() {
        $projectModel = new Project();
        $projects = $projectModel->find('all');
        return $projects;
    }

}

Returning that data in Example Controller

// once inside your correct view function just do:
$projects = $this->Example->allProjects();
$this->set('projects', $projects);

In the Example view

<?php
// Now assuming you're in the .ctp template associated with
// your view function which used: $projects = $this->Example->allProjects();
// you should be able to access the var: $projects
// For example:
print_r($projects['Project']);

Why is this "better" practice than loading both models into your controller? Well, the Project model is inherited by the Example model, so Project data now becomes part of the Example model scope. (What this means on the database side of things is the 2 tables are joined using SQL JOIN clauses).

Or as the manual says:

One of the most powerful features of CakePHP is the ability to link relational mapping provided by the model. In CakePHP, the links between models are handled through associations. Defining relations between different objects in your application should be a natural process. For example: in a recipe database, a recipe may have many reviews, reviews have a single author, and authors may have many recipes. Defining the way these relations work allows you to access your data in an intuitive and powerful way. (source)

like image 45
DrewT Avatar answered Sep 21 '22 05:09

DrewT