2009年7月14日星期二

[fw-mvc] Re: Autoloader issue

Matthew Weier O'Phinney wrote:
> -- Jens Kleikamp <jens@codes-concepts.com> wrote
> (on Friday, 10 July 2009, 02:43 PM +0200):
>> I am using ZF 1.8 and have a small problem with the new autoloader.
>>
>> The include_path contains two dirs:
>>
>> /path/to/webapp/library
>> /path/to/webapp/application/doctrine/models
>>
>> And I have three modules:
>>
>> /path/to/webapp/application/modules/shop
>> /path/to/webapp/application/modules/user
>> /path/to/webapp/application/modules/cms
>>
>>
>> In my doctrine models directory is a "UserTable.php" file which contains
>> class "UserTable". If I try to load that class it fails because the zf
>> autoloader uses the module_autloader for the "user module".
>>
>> If I try to load ProductTable class which resides in the same directory
>> it works fine because there is no module with the name "product" in
>> place.
>
> I've been playing with Doctrine lately, and found that it was easiest to
> write my models using vendor prefixes so that the ZF autoloader could
> find them.
>
> As an example, in my modules, I'll have a models directory, and all
> models within are prefixed with <Modulename>_Model_:
>
> application/
> modules/
> scrum/
> models/
> Backlog.php -> Scrum_Model_Backlog
> BacklogTable.php -> Scrum_Model_BacklogTable
>
> This is, of course, just following ZF guidelines.
>
> Then, when configuring Doctrine, I only need to tell it to autoload my
> models and tables:
>
> $manager->setAttribute(
> Doctrine::ATTR_MODEL_LOADING,
> Doctrine::MODEL_LOADING_CONSERVATIVE
> );
> $manager->setAttribute(
> Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES,
> true
> );
>
> I do *not* setup the Doctrine autoloader, however -- this allows the ZF
> autoloading to work. With the above settings, then, Doctrine will not
> try to manually load the models or table classes, but instead utilize
> the autoloader -- and everything resolves perfectly.
>
> This also ensures you don't need to utilize the fallback autoloader
> functionality of the ZF autoloader, and prevents naming collisions. :)
>
>> I tried to modify Zend/Loader/Autoloader.php to fix that, but I don´t
>> think its a good solution - but it works. Please see attachment for the
>> diff.
>>
>> I really don´t like this workaround and would like to know if there is
>> another approach to solve the issue.
>
Matthew, thank you very much,

your solution works like a charm!

With your help I´m now much more happier with the integration of
doctrine into my zf applications.


Best Regards
-Jens

没有评论: