Wednesday, April 15, 2015

CakePHP (1.3) - More than 150 "nested" function calls (xdebug) on all requests -> caused by model relations

Hey guys,

I recently noticed a weird behavior of Cake that was logged by NewRelic. I recently installed Xdebug and got a warning that more than 100 nested function calls were made. I know that I can raise the limit that triggers the error, but that would just be masking the problem.

In my app I have about 120 models. These models are nearly all related to each other, some directly and some through other model relations. That is ok and it is working very well. But when I got that warning, I looked into the request-trace and found out, that on a simple request to the app CakePHP will instanciate EVERY model that is triggered by a call. 

My question is: Is this really the intended behavior of Cake? Why would Cake invoke models and register/instanciate them even if they won't be used in any way in my controllers/models?

I setup recursive to -1, I set debug to 0 and still every (in)directly related model will be invoked. This is the debug-trace:

Model::__constructLinkedModel() - CORE/cake/libs/model/model.php, line 659  Model::__createLinks() - CORE/cake/libs/model/model.php, line 629  Model::__construct() - CORE/cake/libs/model/model.php, line 482  ClassRegistry::init() - CORE/cake/libs/class_registry.php, line 142  Model::__constructLinkedModel() - CORE/cake/libs/model/model.php, line 666  Model::__createLinks() - CORE/cake/libs/model/model.php, line 629  Model::__construct() - CORE/cake/libs/model/model.php, line 482  ClassRegistry::init() - CORE/cake/libs/class_registry.php, line 142  Model::__constructLinkedModel() - CORE/cake/libs/model/model.php, line 666  Model::__createLinks() - CORE/cake/libs/model/model.php, line 629  Model::__construct() - CORE/cake/libs/model/model.php, line 482  ClassRegistry::init() - CORE/cake/libs/class_registry.php, line 142  Controller::loadModel() - CORE/cake/libs/controller/controller.php, line 637  Controller::constructClasses() - CORE/cake/libs/controller/controller.php, line 493  Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 186  Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171  [main] - APP/webroot/index.php, line 83

This is done for nearly 100 models, and depending on the relations it can get worse.

So is it really "normal" that Cake constructs every model that is related to the targeted model I use in my controller?

When I remove the relations, the app gets insanely fast and does what it should - a simple find on my targeted model. 

Does anyone have a clue how one could influence this behavior?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: