CakePhP 3: Using $this->loadModel for an unrelated Model in Controller

Dear CakePhP Forum Members

I have been using CakePhP for 2 months now and have to say it’s been a revelation! It’s a great framework.

My problem is: I currently have a need to load a model in a controller for a completely unrelated (SQL wise) model. Here is my basic ‘Races’ index controller method:

public function index()
    $this->set('races', $this->Races->find('all', ['contain' => ['Countries'], 'order' => ['' => 'ASC']]));

    $page = $this->Pages->find('all', ['conditions' => ['Pages.current_url LIKE' => '/races/']]);
    $this->set('page', $page->first());

    $this->set('_serialize', ['races','page']);


I am then trying to have the following in the ‘Races’ index.ctp

<?= h($page->body) ?>

Which brings up an error - what am I doing wrong?

All/any help most grateful received!


What is the error you are getting?

Sorry - should have put that up!

Notice (8): Undefined property: Cake\ORM\Query::$body [APP/Template\Races\index.ctp, line 13]
Code Context
include - APP/Template\Races\index.ctp, line 13
Cake\View\View::_evaluate() - CORE\src\View\View.php, line 992
Cake\View\View::_render() - CORE\src\View\View.php, line 952
Cake\View\View::render() - CORE\src\View\View.php, line 587
Cake\Controller\Controller::render() - CORE\src\Controller\Controller.php, line 611
Cake\Routing\Dispatcher::_invoke() - CORE\src\Routing\Dispatcher.php, line 120
Cake\Routing\Dispatcher::dispatch() - CORE\src\Routing\Dispatcher.php, line 87
[main] - ROOT\webroot\index.php, line 36

The error message itself showing you the solution. There is no body property for $page. If you are expecting body property try to debug $page.

You are using 'all' instead of ->first() at the end… Do you want all pages or just one?


THANKS Lorenzo! :slight_smile: