ZF-4167: Zend_Db_Table_Row_Abstract does not properly rely on autoloading

Description

Zend_Db_Table_Row_Abstract->findDependentRowset() and Zend_Db_Table_Row_Abstract->findParentRow() rely on Zend_loader::loadClass() for loading dependent and parent tables. This makes it impossible to use autoloading to find these classes and forces the developer to add all paths that may contain DB Table class to be added to the include_path.

In my application I have a custom loader which I registered with Zend_Loader::registerAutoload('My_Model_Loader'). However this loader never gets fired when working with dependent and parent tables because autoloading is not used within Zend_Db_Table_Row_Abstract.

Autoloading should be used before directly trying to load a class. With the proposed MVC filder structure there would be a model folders for each application module. Adding each model folder as an include path is less than ideal. Following a pattern similar to controller actions, it would be reasonable that someone would want to load a model from a specific module.

I'm able to get my application to work just fine by commenting out the try...catch that tries to load the class.

Comments

Linking these issue as they report the same problem

Hi,

I would really like to see this included, also I believe this could become a blocker to Matthews Resource Autoloader proposal as this uses the autoloader.

Thx

Keith

Attaching a patch for this issue, this is just a proof of concept for now and therefore no unit tests etc. This may be a good temporary fix until all the class loading problems have been sorted out.

Patch Purpose

Remove Zend_Loader dependency

Patch Details

  • Added new protected static property $_loader
  • Added new public static method setLoader()
  • Added new public static method loadClass()
  • Added new public static method getLoader()
  • Added new protected static method _setupLoader()

Patched From std trunk

The idea is that it works in a similar way to the setDefaultAdpater, the loader is either null (use autoloader) or a valid callback array. All the Zend_Db_Table classes then use Zend_Db_Table_Abstract::loadClass() to load their classes.

I am currently creating a new patch which is simpler after talking to Matthew about it.

Right here is my second attempt at patching this issue :)

I have simplified by just wrapping the Zend_Loader class in class_exists that use the autoloader.

Patch is from the 1.7.6 release and includes:

Patched Db Table files Unit tests Documentation

Hopefully I have got it right, see what you think.

This issue affects the use of the Zend_Application_Module_Autoloader in ZF 1.8.0.

The easiest way to fix this is to change the following in Loader.php

if (class_exists($class, false) || interface_exists($class, false)) {

to

if (class_exists($class) || interface_exists($class)) {

any ideas why it does not check the autoloader by default?

Resolved in trunk and 1.8 release branch