duanpasi6287 2012-07-31 16:06 采纳率: 100%
浏览 53
已采纳

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

I have looked on here and found some people having some of the same problems but none of there solutions seemed to work for me.

Now I am trying to add a new page into a website I have been given to update (I did not start the site form new, myself). Now with the same table(s) has what is in my live site, I use this code with a new copy of cakePHP. However I get 'Call to a member function find() on a non-object' and this might, i think, have something to do with the way i am calling the find command or the way cakePHP config file might have been changed.

Add this to my Routes.php :

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

My Model code :

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

This code points, and i think checks all my db fields, this is code I copied from another model but changed it for the use with my table.

My Controller Code :

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

Screen Error :

        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

The only two table this deals with are events & teams - They are saved as lower case, and with 's' at the end. The teams table was already in the db, events table is the new I made. Like I say when I use these table, exported from the live site, to a test db, then using a new CakePHP setup, is works, it prints the tables contents.

So It must be something to do with the way CakePHP is setup and this (cake) is still very new to me. This is the 1s time I have ever made a new model/controller/table. All I have been doing is adding new functions to already built & working controllers.

I have also tried this code, '$EventData = $this->set('events', $this->Event->find('all'));' I got '$this->set('events'' from another post on this type of error

I have tried to be as clear as possible, please let me know if I have not been.

Many Thanks For Any Help!

Glenn Curtis.

  • 写回答

1条回答 默认 最新

  • douqin6785 2012-07-31 16:09
    关注

    Remove:

    var $uses = array();
    

    CakePHP has it's own inheritance model. As such, $uses is rather counterintuitive.

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

    By default, CakePHP implicitly sets $uses to the corresponding model of the controller, in your case Event. So simply removing it is the Cake way to do it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像