Because I need it in every controller, I have added 'User' to
AppController::uses. But then something strange happened: When calling
my SearchController, I got the error that the table searches for the
model Search doesn't exist. The funny thing: there IS no Search model.
I tracked the problem down to the following cause... but let me
explain the context first.
My SearchController is able to search in any models that are specified
in the following var:
Configure::write('Zivi.search.models', array(
'admin' => array('User', 'Problem') // When in 'admin' context,
search in these models...
,'lib' => array('Medium', 'Author') // etc...
,'users' => array('User')
,'inv' => array('Hardware')
,'term' => array('TermolinoWord')
,'contr' => array('Contract')
));
The context is set using the URL parameter "layout", and the models to
search are set using an overridden constructClasses() method:
function constructClasses() {
$conf = Configure::read($this->usesConfigureKey);
$layout = $this->__getLayout();
if (!empty($conf[$layout])) {
$this->uses = $conf[$layout];
} else {
trigger_error(get_class($this).' is not using any models with
layout "'
.$layout.'", you have to configure it in core.php: "'
.$this->usesConfigureKey.'"', E_USER_WARNING);
}
return parent::constructClasses();
}
This just sets the $this->uses variable to one of the arrays in the
"Zivi.search.models" setting.
So far, so good. Everything worked before I added the User model to
AppController::uses. But as said, after adding it, CakePHP complained
that the table searches is missing.
The problem is in Controller::__mergeVars(), look at the last line of
the following code:
function __mergeVars() {
$pluginName = Inflector::camelize($this->plugin);
$pluginController = $pluginName . 'AppController';
if (is_subclass_of($this, 'AppController') || is_subclass_of($this,
$pluginController)) {
$appVars = get_class_vars('AppController');
$uses = $appVars['uses'];
$merge = array('components', 'helpers');
$plugin = null;
if (!empty($this->plugin)) {
$plugin = $pluginName . '.';
if (!is_subclass_of($this, $pluginController)) {
$pluginController = null;
}
} else {
$pluginController = null;
}
if ($uses == $this->uses && !empty($this->uses)) {
if (!in_array($plugin . $this->modelClass, $this->uses)) {
array_unshift($this->uses, $plugin . $this->modelClass);
This makes that a model "Search" is added to the $this->uses variable,
because $uses is Array('User') and $this->uses is Array('User'), too!
So it adds $plugin.$this->modelClass to $this->uses, while $plugin is
empty (don't really know what's this for) and $this->modelClass is
"Search".
I don't really know why CakePHP adds this model to the $this->uses
variable - SearchController DOESN'T have a primary model, so it
shouldn't be added automatically, should it? Or does CakePHP enforce
every controller to have a primary model?
At the time being I solved the problem by adding another model to be
searched instead of only User:
Configure::write('Zivi.search.models', array(
...
,'users' => array('User', 'UserProfile')
...
));
This prevents addition of the Search model. It's working, but I'm
still wondering where's the real problem here. Maybe I'm doing
something in an unclean way?
Thanks a lot for replies... This one really freaked me out some
hours. ;-)
Josh
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
No comments:
Post a Comment