1

I've integrated Cake's Auth component into my app. 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()
File: C:\xampp\htdocs\ips-mvc\lib\Cake\Model\Datasource\DboSource.php
Line: 1063

Any idea what this means?

For the record, I do have a model in my app called 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? 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.

Any ideas?

1
  • 1
    I went ahead and changed my Security model to another name and the problem seems to have solved itself now but an explanation would still be appreciated. Commented Nov 5, 2013 at 10:46

1 Answer 1

1

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).

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:

 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.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.