0

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?

1
  • Does your Product model actually have a Category attribute? Commented May 12, 2012 at 12:38

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.