【问题标题】:Cakephp Fatal error: Call to a member function find() on a non-objectCakephp 致命错误:调用非对象上的成员函数 find()
【发布时间】:2012-05-12 12:34:37
【问题描述】:

我收到以下错误:
注意(8):未定义属性:ProductsController::$Category **

致命错误:在非对象上调用成员函数 find()

**
在第 7 行的 \shop\app\controllers\products_controller.php
型号:

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

控制器:

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'));
} 

我有两个控制器类 1。 CategoriesController 2。 产品控制器。当我在类别控制器中使用列表方法时, 它的工作,但在产品控制器它给出错误?

【问题讨论】:

  • 你的Product 模型是否真的有Category 属性?

标签: cakephp


【解决方案1】:

如果您想从 Category(或其他关联)模型中调用方法,您必须通过“父”(在本例中为 Prodcut)模型:

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

应该可以。

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

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    相关资源
    最近更新 更多