CakePHP问题:在非对象上调用成员函数find()

时间:2012-07-31 16:06:53

标签: php cakephp

我在这里看了一下,发现有些人遇到了一些相同的问题,但没有一个解决方案对我有用。

现在我正在尝试将新页面添加到我更新的网站中(我没有自己启动网站表单)。现在,我的实时网站中有相同的表格,我将此代码与cakePHP的新副本一起使用。但是我得到'调用非对象上的成员函数find()',这可能,我认为,这与我调用find命令的方式或者cakePHP配置文件可能已被更改的方式有关。 / p>

将此添加到我的Routes.php:

         Router::connect('/events', array('controller' => 'events', 'action' => 'eventDetails'));

我的型号代码:

   class Event extends AppModel {
var $name = 'Event';

var $validate = array(
    'id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            'allowEmpty' => true,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'startdate' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'endate' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            'allowEmpty' => true,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'spaces' => array(
        'alphanumeric' => array(
            'rule' => array('alphanumeric'),
            //'message' => 'Your custom message here',
            'allowEmpty' => true,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'info' => array(
        'alphanumeric' => array(
            'rule' => array('alphanumeric'),
            //'message' => 'Your custom message here',
            'allowEmpty' => true,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'info' => array(
        'alphanumeric' => array(
            'rule' => array('alphanumeric'),
            //'message' => 'Your custom message here',
            'allowEmpty' => true,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'img' => array(
        'alphanumeric' => array(
            'rule' => array('alphanumeric'),
            //'message' => 'Your custom message here',
            'allowEmpty' => true,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed

var $belongsTo = array(
    'Team' => array(
        'className' => 'Team',
        'foreignKey' => 'team_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
);
}

这段代码指出,我认为检查我的所有数据库字段,这是我从另一个模型复制的代码,但是将其更改为与我的表一起使用。

我的控制器代码:

class EventsController extends AppController {
var $name = 'Events';
var $uses = array();

function eventDetails() {

    $EventData = $this->Event->find('all');

    print_r($EventData);


    die();      
} //End of function eventDetails()

 } // End of EventsController Class

屏幕错误:

        Notice (8): Undefined property: EventsController::$Event        [APP/controllers/events_controller.php, line 10] Fatal error: Call to a member function   find() on a non-object in /var/www/vh/app/controllers/events_controller.php on line 10

此处涉及的唯一两个表是事件&团队 - 他们被保存为小写,最后是's'。团队表已经在db中,事件表是我制作的新表。就像我说当我使用这些表,从实时站点导出到测试数据库,然后使用新的CakePHP设置,它工作,它打印表内容。

所以它必须与CakePHP的设置方式有关,这个(蛋糕)对我来说仍然是一个新手。这是我制作新型号/控制器/表的1s时间。我所做的就是为已经构建的&添加新功能。工作控制员。

我也试过这段代码'$ EventData = $ this-> set('events',$ this-> Event-> find('all'));'我得到了'$ this->设置('event''来自另一篇关于此类错误的帖子

我试图尽可能清楚,如果我没有,请告诉我。

非常感谢您的帮助!

Glenn Curtis。

1 个答案:

答案 0 :(得分:1)

卸下:

var $uses = array();

CakePHP拥有自己的继承模型。因此,$uses相当违反直觉。

$uses = array('Event', 'Foo'); // explicitly use Event and Foo model
$uses = array(); // don't use any models

默认情况下,CakePHP隐式将$uses设置为控制器的相应模型,在您的情况下为Event。所以简单地删除它就是 Cake 的方法。