I’m using CakePHP 2.0. I have 2 models/controllers (Category/Coupon):
// File: Model/Coupon.php
// Table Name: coupons
class Coupon extends AppModel {
public $displayField = 'Coupon';
}
// File: Model/Category.php
// Table Name: categories
class Category extends AppModel {
public $displayField = 'Category';
}
// File: Controllers/CouponsController.php
class CouponsController extends AppController {
}
// File: Controllers/CategoriesController.php
class CategoriesController extends AppController {
}
// File: Controllers/AppController.php
class AppController extends Controller {
public $helpers = array('Html', 'Form');
public function beforeFilter() {
//$this->set('cat', $this->Category->find());
$this->set('cat', $this->Coupon->find());
}
}
I’m trying to set a variable for the default.ctp view in the AppController. When I set ‘cat’ to: “$this->Coupon->find()”, it returns an array that I can print out in in my view. When I set ‘cat’ to the commented line “$this->Category->find()”, I get the following error:
Fatal error: Call to a member function find() on a non-object in …
I’ve tried error handling this a ton, but I can’t seem to figure it out. Am I missing something?
It sounds like your model is not loaded before trying to access it. You need to make sure that both models are loaded in the controller. You can do this in one of multiple ways.