1

我看过这里,发现有些人有一些相同的问题,但似乎没有一个解决方案对我有用。

现在我正在尝试将一个新页面添加到一个我已经被更新的网站中(我自己并没有从新的网站表单开始)。现在,同一个表具有我的实时站点中的内容,我将此代码与 cakePHP 的新副本一起使用。但是,我得到“调用非对象上的成员函数 find()”,我认为这可能与我调用 find 命令的方式或 cakePHP 配置文件可能已更改的方式有关。

将此添加到我的 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”结尾。团队表已经在数据库中,事件表是我制作的新表。就像我说的,当我使用从实时站点导出的这些表到测试数据库,然后使用新的 CakePHP 设置时,它是有效的,它会打印表的内容。

所以这一定与 CakePHP 的设置方式有关,而且这个(蛋糕)对我来说还是很新的。这是我第一次制作新模型/控制器/表。我一直在做的就是为已经构建和工作的控制器添加新功能。

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

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.

4

1 回答 1

1

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.

于 2012-07-31T16:09:06.140 回答