带有$的非直观行为在CakePHP中使用继承
前几天我遇到了一个问题,带有$的非直观行为在CakePHP中使用继承,php,cakephp,Php,Cakephp,前几天我遇到了一个问题,PagesController抛出以下错误: 致命错误:调用成员函数 在中的非对象上查找() /home/jmccreary/www/thoroughbredsource.com/cakephp/app/app_controller.php 第20行 app\u controller.php第20行: $this->current_user = $this->User->find('first', array('recursive' => 0,
PagesController
抛出以下错误:
致命错误:调用成员函数
在中的非对象上查找()
/home/jmccreary/www/thoroughbredsource.com/cakephp/app/app_controller.php
第20行
app\u controller.php第20行:
$this->current_user = $this->User->find('first', array('recursive' => 0,
'conditions' => array('User.id' => $this->logged_in_user_id)));
在本例中,$this
是PagesController
的一个实例,但出于任何原因,它没有从app_controller.php继承用户
模型
为完整起见,以下是相关代码:
pages\u controller.php
var $name = 'Pages';
var $uses = null;
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('*');
}
var $uses = array('User');
var $components = array('Session', 'Cookie', 'RequestHandler', 'DebugKit.Toolbar', 'Auth' => array('autoRedirect' => false, 'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'), 'flashElement' => 'error', 'loginError' => 'The username or password you provided are incorrect.', 'authError' => 'Please log in first.', 'fields' => array('username' => 'email', 'password' => 'passwd'), 'userScope' => array('User.active' => 1)));
var $helpers = array('Html', 'Form', 'Session');
function beforeFilter() {
parent::beforeFilter();
// configure Cookie Component
// ...
$this->logged_in_user_id = $this->Auth->user('id');
if ($this->logged_in_user_id) {
// NOTE: the following runs on each request for the logged in user
$this->current_user = $this->User->find('first', array('recursive' => 0, 'conditions' => array('User.id' => $this->logged_in_user_id)));
}
}
app\u controller.php
var $name = 'Pages';
var $uses = null;
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('*');
}
var $uses = array('User');
var $components = array('Session', 'Cookie', 'RequestHandler', 'DebugKit.Toolbar', 'Auth' => array('autoRedirect' => false, 'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'), 'flashElement' => 'error', 'loginError' => 'The username or password you provided are incorrect.', 'authError' => 'Please log in first.', 'fields' => array('username' => 'email', 'password' => 'passwd'), 'userScope' => array('User.active' => 1)));
var $helpers = array('Html', 'Form', 'Session');
function beforeFilter() {
parent::beforeFilter();
// configure Cookie Component
// ...
$this->logged_in_user_id = $this->Auth->user('id');
if ($this->logged_in_user_id) {
// NOTE: the following runs on each request for the logged in user
$this->current_user = $this->User->find('first', array('recursive' => 0, 'conditions' => array('User.id' => $this->logged_in_user_id)));
}
}
我通过changevar$uses=null克服了这个错误
tovar$uses=array()代码>。请注意,完全删除此行会导致相同的错误
最后,我没有完全理解最初的问题或我的解决方案。我希望能有更好的解释或适当的解决办法。顺便说一句,根据控制器类的源代码运行CakePHP 1.3.10。,您可以在以下位置看到:
通过使用$uses=array()
,您告诉PagesController“不使用模型并与AppController'$uses'合并”,这样它就可以工作了。您可以在第399行开始的源代码中看到合并。据我所见,它将“null”视为“false”,这意味着它不会加载任何模型,包括来自AppController的模型
您还可以查看有关$uses
的更多信息,您正在扩展AppController,然后将uses变量设置为false/null,这告诉它根本不使用任何模型
如果将其设置为空数组(),则将从PagesController中获得一个空数组,该数组与AppController中的数组合并,并且它的行为将与预期的一样
这清楚地记录在手册中。+1在我看来,这几乎是对OOP正常工作方式的一种令人不安的篡夺。奇怪的是第427行:elseif($This->uses!==null | | This->uses!==false){$merge[]='uses'}
在此处使用|
而不是&&
,将始终导致为true
@webbiedave-您读得不对。它说:如果它是空的,或者如果它是假的。换句话说,在本例中,它将null视为false。就像我说的,如果它是假的,它不会与AppController合并。因此,如果null等于false,那么null也意味着它不会被合并。+1谢谢,很抱歉没有RTFM。不过我同意webbiedave的观点,这似乎打破了传统的OOP继承。不管这是PHP作为一种语言还是CakePHP都是另一回事。@Francois:$this->uses
有什么可能的值,上面的条件会导致false?明白了。Francois Deschenes已经提供了上述答案,我接受了。