简体   繁体   English

使用CakePHP的Auth组件登录时出错

[英]Error when logging in using CakePHP's Auth component

I've integrated Cake's Auth component into my app. 我已经将Cake的Auth组件集成到我的应用程序中。 It mostly seems to work ok but I quite often get an error when logging in looking something like this: 似乎大多数情况下都可以,但是登录时我经常会出现错误:

Error: Call to undefined method Security::getDataSource() 错误:调用未定义的方法Security :: getDataSource()
File: C:\\xampp\\htdocs\\ips-mvc\\lib\\Cake\\Model\\Datasource\\DboSource.php 文件:C:\\ xampp \\ htdocs \\ ips-mvc \\ lib \\ Cake \\ Model \\ Datasource \\ DboSource.php
Line: 1063 线:1063

Any idea what this means? 知道这意味着什么吗?

For the record, I do have a model in my app called Security . 作为记录,我的应用程序中确实有一个名为Security的模型。 I wasn't aware of any reserved model names in Cake but is there a chance my Security mdoel is conflicting with a Cake component? 我不知道Cake中有任何保留的模型名称,但是我的Security mdoel是否有可能与Cake组件冲突? This error can still occur when the Security model is not used although it is quite sporadic in its appearance - sometimes refreshing the page will make everything work fine. 当不使用“ Security模型时,尽管它的外观是非常零散的,但仍然会发生此错误-有时刷新页面会使一切正常。

Any ideas? 有任何想法吗?

The error is caused by passing a component instance to a method expecting a model instance, and are easily avoidable by not creating model classes with the same name as a component in use. 该错误是由于将组件实例传递给需要模型实例的方法而引起的,并且可以通过不创建与使用中的组件同名的模型类来轻松避免该错误。

Models and Components cannot have the same name 模型和组件不能使用相同的名称

The problem is not related to duplicate class names as Models do not have a class name suffix, yet components do. 该问题与重复的类名无关,因为模型没有类名后缀,而组件却有。

However, for example, in a controller the following syntax: 但是,例如,在控制器中,以下语法:

$this->{$alias}

Is used to access both a component (class name {$alias}Component ) or a model (class name $alias ). 用于访问这两个组件(类名{$alias}Component模型(类名$alias )。

As such, having a model with the same name as a component makes one or the other inaccessible. 因此,拥有与组件同名的模型将使一个或另一个无法访问。

Cache Poisoning 缓存中毒

However the problem doesn't stop there. 但是问题不止于此。 Objects are stored in the class registry using an alias as the key: 使用别名作为键对象存储在类注册表中

For example, in Model : 例如,在Model中

 ClassRegistry::addObject($this->alias, $this);

If the same alias is used (Security model, SecurityComponent ) - the object occupying the key "Security" is simply the first one to get added to the registry - all subsequent requests for "Security" will return that object. 如果使用相同的别名(安全性模型, SecurityComponent )-占用键“安全性”的对象只是添加到注册表中的第一个对象-所有对“安全性”的后续请求都将返回该对象。

Depending on cache expiration and whether the model and component are used in the same request - determines how often errors such as the one in the question appear and whether they are "random" or reproducible. 根据缓存的过期时间以及在同一请求中是否使用模型和组件,确定错误(例如问题中的错误)出现的频率以及它们是“随机的”还是可再现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM