Added by Thomas VEQUAUD, last edited by Thomas VEQUAUD on Jan 26, 2010  (view change)

Labels

 

Zend Framework: Zend_Generator_Orm Component Proposal

Proposed Component Name Zend_Generator_Orm
Developer Notes http://framework.zend.com/wiki/display/ZFDEV/Zend_Generator_Orm
Proposers Thomas VEQUAUD
Revision 0.1 - 27 April 2009: Creation of the proposal
0.2 - 27 April 2009: Attachment added (source code in a zip file)
0.3 - 27 April 2009: Sources updated
0.4 - 28 April 2009: Requirements and milestones added
0.5 - 30 April 2009: Class squeletons added
0.6 - 02 May 2009: Use cases added
0.7 - 14 May 2009: Use cases updated
0.8 - 26 January 2010: New version released (wiki revision: 33)

Table of Contents

1. Overview

Zend_Generator_Orm maintains updated the models of a database schema in a database. This ensures that all changes applied to your database schema are automatically applied to your models classes without rewrite your own methods inside.

Within Zend Framework, you need to define classes in 'models' directory that extends Zend_Db_Table_Abstract for each table in your database that you want to access. For each class, you need to declare various protected variables like the name of the table in $_name, the primary(ies) key(s) in $_primary, the dependent tables $_dependentTables, the linked tables in $_referenceMap, etc. At least, you spent a lot of time to create the same things and at each modification of your database, you have to edit again your models!
Zend_Generator_Orm proposal is a simple component that reads the architecture of your database and generates Zend models automatically. This generator creates and/or updates your models with predefined methods and keep intact your own implemented methods in it.

2. References

3. Component Requirements, Constraints, and Acceptance Criteria

Having not the possibility to know by programming with a describeTable if the field is a foreign key, I had to fiddle with a naming convention. This naming convention allows to fix this problem. These rules seem to me basic in the database development because by this way you can easily identify the foreign keys, the table where they come from and every field is unique :

  • tables names : lowercase, only letters (category for example);
  • fields names : lowercase, singular, prefixed by the table name followed by an underscore (category_name for example);
  • foreign keys keep their name of primaries keys : by this way, with a regexp, I can know if it's a foreign key and which table this id comes from.

Acceptance criteria

  • This component will correctly read a database architecture.
  • This component will generate and update the models class within zend models directory.
  • Updates will not rewrite the methods implemented in every model class.
  • This component will work for all databases.

4. Dependencies on Other Framework Components

  • Zend_Db
  • Zend_Db_Table
  • Zend_Exception

5. Theory of Operation

Load the class 'Zend_Orm' and instantiate it... Then just call the methods update() and/or create().
You have an example below...

6. Milestones / Tasks

Milestones :

  1. [DONE] Developing the ORM generator
  2. [DONE] Packaging for Zend architecture
  3. [DONE] Suggesting it on Zend Framework Proposals
  4. Working prototype checked into the incubator supporting use cases #1

7. Class Index

  • Zend_Orm
  • Zend_Generator_Exception
  • Zend_Generator_Orm_Abstract
  • Zend_Generator_Orm_Exception

8. Use Cases

The main use case is when you need to change the database schema of an application : you have to update or create a model for each table in your database schema... Busy work!
Zend_Generator_Orm makes it for you.
UC-01

Database configuration
Implement a controller 'OrmController' in your application/controller directory in your own Zend Application :

Implement a view 'index' in your application/views/scripts/orm directory :

In your browser, call the URL http://root_url/url_base/orm/ and then check a possible exception. Blank page instead? Congratulation! You can go the UC-02...

UC-02

Create new models with Zend_Orm component
Add the create action in the controller OrmController previously implemented :

In your browser, call the URL http://root_url/url_base/orm/create and then check :

  1. The content of your application/model directory.
  2. The content of a chosen class model.
UC-03

Update models previously created by Zend_Orm component
Modify the structure of your database.
Modify (before and after the separator) the content of the chosen class model of the generated class models :

Add the update action in the controller OrmController previously implemented :

In your browser, call the URL http://root_url/url_base/orm/update and then check the content of your modified model.

9. Class Skeletons

Zend/Orm.php

Zend/Generator/Exception.php

Zend/Generator/Orm/Abstract.php

Zend/Generator/Orm/Exception.php

In the methode "write_dependentTable" use 'array('.implode('\',\'',$properties['dt']). ')'
for convert array

Hi Sylvain,

Thank you for your comment, I'll consider it when I'll have a moment to work on it.

Actually I didn't work a lot on this project for a long time. I just took a look on the code 2 weeks ago and I afraid myself : I was a beginner on Zend when I created Zend_Generator_Orm so I made a lot of huge mistakes! I modified a lot of things this weekend and I'll release a new version... Soon, I hope, when I'll have the time to work a little bit more and test it on MySQL, PostgreSQL and Oracle.