Skip to end of metadata
Go to start of metadata

Zend Framework: Zend Tool and Doctrine 1 Integration Component Proposal

Proposed Component Name Zend Tool and Doctrine 1 Integration
Developer Notes http://framework.zend.com/wiki/display/ZFDEV/Zend Tool and Doctrine 1 Integration
Proposers Benjamin Eberlei
Zend Liaison TBD
Revision 1.0 - 9th November 2009 (wiki revision: 6)

Table of Contents

1. Overview

This Proposal aims to integrate Zend Framework and Doctrine 1 via Zend Tool.
All Doctrine 1 CLI commands will be integrated into the Zend_Tool project context
and additional context resources are provided to support Doctrine metadata,
migrations, fixture etc. Both non-modular and modular applications should
be supported for the task of generating models and tables. This proposal
sets on top of Doctrine 1 resource proposal by Matthew Lurz.

2. References

3. Component Requirements, Constraints, and Acceptance Criteria

  • This component MUST use Doctrine 1 Resource for Zend_Application
    • This resource SHOULD be usable with both Modular and non-modular MVC application.
  • This component MUST integrate all Doctrine 1 CLI Tasks
  • This component SHOULD offer additional Zend Project Context Resources that are required for Doctrine.
  • This component SHOULD offer further providers that help the integration of ZF and Doctrine
    • This component SHOULD make use of other Zend components for this goal.
    • This component SHOULD support prototyping of forms
    • This component MAY support Zend_Test and Doctrine integration.

4. Dependencies on Other Framework Components

  • Zend_Application
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Form
  • Zend_CodeGenerator
  • Doctrine 1.2

5. Theory of Operation

The Zend Tool integration will use the configured Zend Application resource
to configure Doctrine 1 for the use with Schema Tool or other CLI tasks.

With a configured Doctrine resource you will be able to issue the following commands:

Commands will all make heavy use of --verbose and --pretend flags.

You should be able to configure via the doctrine resource if you want to use code-generation
to generate abstract classes for forms or tables of Doctrine_Record's.

Usage would look like:

Zend Tool would keep track of the Doctrine context directories and will make sure that the generation
is taking place as configured. If you want to generate different records/tables into different modules
you have to configure the modules accordingly, this will take place in a new config file being called
"doctrine-modules.ini" which has the Doctrine Model <-> ZF Module relationsships and can either be
edited by hand or be configured with the help of "zf configure doctrine.module" and "zf assign-model doctrine.module".

It will now generate the classes in ZF-Style (Supported by Resource Loader):

Or in Pear Style:

With the help of Doctrine Model and Table context files it is possible to find orphaned files at this
point and delete them.

All this is up for discussion and changes, its just an outline over what I think is possible and could be done to integrate Zend Tool and Doctrine 1. What do you think?

6. Milestones / Tasks

  • Milestone 1: Community Review
  • Milestone 2: Prototype
  • Milestone 3: Zend Acceptance
  • Milestone 4: Completion & Documentation

7. Class Index

  • Zend_Doctrine_Tool_Provider_Doctrine
  • Zend_Doctrine_Tool_DoctrineTasks
  • Zend_Doctrine_Tool_Context_DataFixturesDirectory
  • Zend_Doctrine_Tool_Context_MigrationsDirectory
  • Zend_Doctrine_Tool_Context_SqlDirectory
  • Zend_Doctrine_Tool_Context_ModelsDirectory
  • Zend_Doctrine_Tool_Context_YamlSchemaDirectory
  • Zend_Doctrine_Tool_Context_ModelFile
  • Zend_Doctrine_Tool_Context_TableFile
  • Zend_Doctrine_Tool_Context_ModelsAbstractDirectory
  • Zend_Doctrine_Tool_Context_ModelAbstractFile
  • Zend_Doctrine_Tool_Context_TableAbstractFile
  • Zend_Doctrine_Tool_Context_DoctrineModuleConfigFile
  • Zend_Doctrine_Tool_Context_FormDirectory
  • Zend_Doctrine_Tool_Context_FormFile
  • Zend_Doctrine_Tool_Context_FormAbstractFile
  • Zend_Doctrine_Form
  • Zend_Doctrine_CodeGenerator_Form
  • Zend_Doctrine_Import
  • Zend_Doctrine_Import_Builder

8. Use Cases

9. Class Skeletons

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Nov 11, 2009

    Hi. I welcome this initiative!
    Why would one need a ini to map modules to models? Matthews resource plugin defines paths in its .ini for each model you wish to create. One is free to point them at a module... I feel I am missing something...
    One of the problems that is consistently emerging is modular support. Could someone be a little verbose on expressing the exact problem of modular support? Also (how) does this tie in with autoloading and or PEAR style model organization (Doctrine 1.2+). Should the model class names reflect the module name? (Model_Base_Entity vs CMS_Model_Base_Entity) Ah, questions, tastes and best practices...

    And some concrete feedback : Proposed is to put doctrine models in the directory 'module/models'. Personally I have other, none-doctrine, model classes at that location (so these can be used from both controllers and amf-services) and therefore place the doctrine models within a directory called doctrine.

    module/models/... (custom model classes)
    module/models/doctrine/...

    1. Nov 11, 2009

      The problem is not ZF not going to find the models, the problem is that Doctrine Schema files don't support the notion of a module. That means you would want to have a means to generate the models into different modules, say you have three modules: Default, Cms and Guestbook.

      Then you have different models, User, Article, Category, Comment, (Guestbook)-Entry.

      You would want to have the following generation of the models:

      This has to be configured in an additional way for the Doctrine Import scripts (CLI ./doctrine generate-models) to build the files correctly.

      For your example you could define the doctrine paths to be:

      with the respective:

      This way these files for this classes won't be mixed with your other model files.

  2. Nov 11, 2009

    Wouldn't want one to be able to only run commands on a single module? I know I would
    example :

    1. Nov 11, 2009

      great idea, i buy it

    2. Nov 11, 2009

      Good catch Bas. I just wanted to point out that care would need to be taken in the case of interdependent modules due to connection binding, see http://www.doctrine-project.org/documentation/manual/1_1/en/yaml-schema-files#features-examples:connection-binding. In these cases, models for 2 or more modules could be bound to same connection.

  3. Dec 12, 2009

    I'd suggest grouping all doctrine tasks under one namespace if possible. It's good practise all along ZF.

     zf doctrine build-all
    
     zf doctrine build-all-reload
    

    etc. This is (at least for me) expected behaviour.

    1. Dec 12, 2009

      Zend Tool has namespaces at third position, for semantic reasons.

      zf <action> <provider>.

      The Provider will be Doctrine, so its all grouped in one namespace.

  4. Dec 14, 2009

    Is it possible to access the code of this proposal? It's all included in the Zym git repository, but it's not accessible at all. Could you point out where we can download the complete Doctrine namespace for Zend Tool? Copy/paste every file from the webinterface is too much work

  5. Dec 15, 2009

    I have another comment, a bit related to the one Bas K posted at first. I think it's a good idea to have the ability configure paths relative to a module's root. In combination with the module specific commands (zf create doctrine <moduleName>) this component will be very flexible in respect of modular loading and generation of models.

    I'd like to have all module specific information inside my module directory. That said, for now I have this set of configuration:

    • scheme file: configs/scheme.yml
    • fixtures: configs/data/fixtures/
    • sql: configs/data/sql/
    • migration: configs/migrations/
    • models: models/
      This means the scheme file for e.g. a guestbook module is located at application/modules/guestbook/configs/scheme.yml

    I took care of the autoloading with the modules application resource (http://juriansluiman.nl/en/blog/article/105/doctrine-modular-loading-of-models). I think the module-based configuration (for both config files and the models/forms) is a more flexible approach to keep the loved ZF modular structure including the Doctrine advantages.

    My comment has several implications:

    • Mapping (configure doctrine.module & assign-model doctrine.module) isn't required anymore, the module controls itself
    • Actions with a module argument are easy and convenient to implement
    • Actions without a module argument loops through all modules to perform the selected action (this might be the negative side of my comment)

    I think there are several options to configure Doctrine such it's intelligent with loading the models in Zend's modular structure. A lot of information is spread lately about the integration of Doctrine with ZF, but 99% forgets the modular structure because Doctrine doesn't provide such an organized structure. I hope with the integration of Doctrine 1 and Zend_Tool (this proposal) we look more at this problem and provide a better method to keep things inside the module directory (imho the preferred method).
    I know my comment doens't only apply to this proposal, but is also meant for the Doctrine resource (http://framework.zend.com/wiki/display/ZFPROP/Zend_Application_Resource_Doctrine+-+Matthew+Lurz). I thought this was the best place to put my ideas down

  6. Mar 08, 2010

    I committed the first working version of my tooling support for ZF + Doctrine prototype to my user branch:

    http://framework.zend.com/svn/framework/standard/branches/user/beberlei/zfdoctrine/

    Using it boils down to the following commands:

    First you need to modify your .zf.ini to look for the DoctrineProvider in "library/Zend/Doctrine/Tool/DoctrineProvider.php"

    Then:

    Now you can go to your application/config/schema directory and create a yaml schema, for example:

    Plus add the module resource loaders to your Bootstrap:

    Here you can see the requirements on the model. ZF and Doctrine Integration only works if you follow it completly:

    Your Models have to be called either Model_Foo or Module_Model_Foo

    Now you can go generate the models from the YAML file:

    The first command generates the Model_User and Addressbook_Model_Contact aswell as their respective Base clases Model_Base_User and Adressbook_Model_Base_Contact. The second command creates the database from the schema and loads a fixture from the "application/configs/fixtures".

    What is missing:

    • Documentation
    • Tool support to create multiple places where a YAML schema can be generated.

    Be aware:

    This proposal tightly integrates ZF and Doctrine in the default project schema. Any attempt to use it in another way will fail, and you should use the components strengths separately to get where you want to.

    I would appreciate feedback from anyone who wants to give it a try.

  7. Mar 11, 2010

    Hello everyone,

    I can't checkout your repository :

    C:\Users\MightyDucks\Desktop\zend\library\Zend\Doctrine2\Application\Resource\Entitymanager.php
    In directory 'C:\Users\MightyDucks\Desktop\zend\library\Zend\Doctrine2\Application\Resource'
    Can't open file
    'C:\Users\MightyDucks\Desktop\zend\library\Zend\Doctrine2\Application\Resource\.svn\tmp\text-base\Entitymanager.php.svn-base':
    The specified file is not found.

    Can you check your repos ?

    Thank you

  8. Apr 26, 2010

    Hi Julien - I had the same problem today. It is because there are two files in that dir (Entitymanager.php and EntityManager.php) which obviously resolve to the same file in windows due to its lack of case sensitivity. There is no way around this at present other than doing the check-out on a non-windows machine. I've sent Benjamin (beberlei) an email asking him to remove the extra file so check-outs work