Added by Thomas VEQUAUD, last edited by Thomas VEQUAUD on May 14, 2008  (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 2008: Creation of the proposal
0.2 - 27 April 2008: Attachment added (source code in a zip file)
0.3 - 27 April 2008: Sources updated
0.4 - 28 April 2008: Requirements and milestones added
0.5 - 30 April 2008: Class squeletons added
0.6 - 02 May 2008: Use cases added
0.7 - 14 May 2008: Use cases updated (wiki revision: 29)

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.

English

Within Zend Framework, you need to define a class 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.

Français

Au sein du Framework Zend, vous avez besoin de définir une classe dans le répertoire 'models' qui étend Zend_Db_table_Abstract pour chaque table de votre base de données que vous voulez accéder. Pour chacune de ces classes, vous avez besoin de déclarer des variables protected comme le nom de la table dans $_name, les clé(s) primaire(s) dans $_primary, les tables dépendantes dans $_dependentTables, les tables liées dans $_referenceMap, etc. Au final, vous passez un temps fou à créer les mêmes choses et, à chaque modification de votre base de données, vous devez encore éditer vos modèles!
Zend_Generator_Orm est un composant simple qui lit l'architecture de votre base de données et génères automatiquement les modèles Zend. Ce générateur de classes fonctionne que ce soit en création ou en update tout en conservant les méthodes implémentées par l'utilisateur.

2. References

3. Component Requirements, Constraints, and Acceptance Criteria

English

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.

Français

N'ayant pas la possibilité de savoir par programmation avec un describeTable si le champs est une foreign key, il a fallu bidouiller un peu. Vous devez donc respecter une règle de nommage qui permet de pallier à ce problème. Ces règles me semblent élémentaires ds le développement de bases de données car de cette manière la lecture devient plus aisée (on identifie d'un seul coup d'oeil les foreign key et leur table de provenance) et chaque champs est unique :

  • noms des tables en minuscule, au singulier, en lettres uniquement (par exemple category) ;
  • noms des champs en minuscule, préfixés par le nom de la table suivi d'un underscore (par exemple category_name) ;
  • les clés étrangères conservent leurs noms de clés primaires : de cette manière, avec une regexp, je peux de suite savoir si il s'agit d'une foreign key et de quelle table vient cet id.

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.
  • This component will implement 'magic' methodes.

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. IBlank 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