Glenn Curtis
Glenn Curtis

Reputation: 659

CakePHP Issue : Call to a member function find() on a non-object

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.

Upvotes: 1

Views: 2337

Answers (1)

Jason McCreary
Jason McCreary

Reputation: 73031

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.

Upvotes: 1

Related Questions