user1041492
user1041492

Reputation: 1

Cakephp Fatal error: Call to a member function find() on a non-object

I get the following errors:
Notice (8): Undefined property: ProductsController::$Category **

Fatal error: Call to a member function find() on a non-object

**
in \shop\app\controllers\products_controller.php on line 7
Model:

class Category extends AppModel {
var $name = 'Category';
var $hasMany = array('Product');
}  

Controller:

class CategoriesController extends AppController{
var $name = 'Categories';}
class ProductsController extends AppController{
var $name ='Products';

function lists(){
$categories = $this->Category->find( 'all',array('order'=>'Category.id ASC'));
} 

i have two controller class 1. CategoriesController 2. ProductsController. when i used lists method in categories controller, it's work, but in products controller it give error?

Upvotes: 0

Views: 7785

Answers (1)

nappo
nappo

Reputation: 594

If you want to call methods from Category (or other associated) model you have to do it through the 'parent' (in this case Prodcut) model:

$categories = $this->Product->Category->find('all', array('order' => 'Category.id ASC'));

should work.

see http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#relationship-types

Upvotes: 3

Related Questions