CakePHP问题:调用非对象上的成员函数find() [英] CakePHP Issue : Call to a member function find() on a non-object

查看:241
本文介绍了CakePHP问题:调用非对象上的成员函数find()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



现在我想添加一个新的页面到一个网站我已经给了更新(我没有开始新的网站形式,我自己)。现在用相同的表有什么是在我的活网站,我使用这个代码与一个新的副本的cakePHP。然而,我得到调用成员函数find()在一个非对象',这可能,我认为,与我调用find命令的方式或者方式cakePHP配置文件可能已更改。 / p>

将此添加到我的Routes.php:

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

我的模型代码:

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

var $ validate = array(
'id'=> array(
'numeric'=> array(
'rule'=> array '$'
'''',
'''''''=''
//'last'=> false,//在此规则后停止验证
//'on'=>'create',//限制验证为'create'
),
),
'startdate'=> array(
'numeric'=> array(
'rule'=> array '),
//'message'=>'您在此处的自定义消息',
//'allowEmpty'=> false,
//'required'=&
//'last'=> false,//在此规则后停止验证
//'on'=>'create',//限制验证为'create'
),
),
'endate'=> array(
'numeric'=>数组(
'rule'=> array('numeric'),
//'message'=>'Your custom message here',
'allowEmpty'=&
//'required'=> false,
//'last'=> false,//在此规则后停止验证
//'on'=>'create' ,//限制验证为'创建'或'更新'操作
),
),
'spaces'=> array(
'alphanumeric'=> array(
'rule'=> array('alphanumeric'),
//'message'=>'Your custom message here'
'allowEmpty'=> true,
//'required'=> false,
//'last'=> false,//在此规则后停止验证
//'on'=>'create',//限制验证为'create'或'update'操作
),
),
'info'=& array(
'alphanumeric'=> array(
'rule'=> array('alphanumeric'),
//'message'=>'Your custom message here'
'allowEmpty'=> true,
//'required'=> false,
//'last'=> false,//在此规则后停止验证
//'on'=>'create',//限制验证为'create'或'update'操作
),
),
'info'=& array(
'alphanumeric'=> array(
'rule'=> array('alphanumeric'),
//'message'=>'Your custom message here'
'allowEmpty'=> true,
//'required'=> false,
//'last'=> false,//停止此规则后的验证
//'on'=>'create',//限制验证为'create'或'update'操作
),
),
'img'=& array(
'alphanumeric'=> array(
'rule'=> array('alphanumeric'),
//'message'=>'Your custom message here'
'allowEmpty'=> true,
//'required'=> false,
//'last'=> false,//在此规则后停止验证
//'on'=>'create',//限制验证为'创建'或'更新'操作
),
),
);
//下面的协会已经创建了所有可能的键,那些不需要的可以删除

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();
} //函数结束eventDetails()

} //结束EventController类

屏幕错误:

 注意(8):未定义的属性:EventsController :: $ Event [APP / controllers / events_controller.php,line 10]致命错误:在第10行的/var/www/vh/app/controllers/events_controller.php中的非对象上调用成员函数find()

这里处理的两个表是事件&团队 - 他们被保存为小写,并在末尾。队表已经在db中,事件表是我做的新。就像我说的,当我使用这些表,从活网站导出到测试数据库,然后使用新的CakePHP设置,是工作,它打印表内容。



所以它必须与CakePHP的设置方式有关,这个(蛋糕)对我来说还是很新的。这是1s时间我做了一个新的模型/控制器/表。我一直在做的是添加新的功能,已经建立&工作控制器。



我也试过这个代码,'$ EventData = $ this-> set('events',$ this-> Event-> find '));'我得到了$ this-> set('events''从另一个帖子这种类型的错误



我试图尽可能清楚



非常感谢任何帮助!



Glenn Curtis。

解决方案

删除:

  var $ uses = array(); 

CakePHP有自己的继承模型, c $ c> $ uses 是违反直觉的。

  $ uses = array 'Foo'); //明确使用Event和Foo模型
$ uses = array(); //不使用任何模型

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


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.

解决方案

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.

这篇关于CakePHP问题:调用非对象上的成员函数find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆