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.

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

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试