ORM with PHP Doctrine
Visual Paradigm for UML (Enterprise Edition) support generates Object-Relational Mapping (ORM) layer source code for PHP with Doctrine framework. In this tutorial we will talk about how to generate and using the generated PHP code to build your database application. Apache web server with PHP module will be used throughout the tutorial.
To generate PHP ORM code with Doctrine framework:
- We first have to define our data model. Let's define our data model using class diagram. Make sure the ORM Persistable stereotype has been specified for the entity classes.

- Select Tools > Object-Relational Mapping (ORM) > Configure Database to open the Database Configuration dialog.

- Switch the language to PHP, and select MySQL database from the list.

- Press the green down arrow button in the Adapter File field to let VP-UML download the adapter file for you. You may need to configure proxy server if you need it for accessing Internal.

- Specify the database connection details inside the DSN section, and then press the Test Connection button to see is the connection configured properly.

- Select Tools > Object-Relational Mapping (ORM) > Synchronize to Entity Relationship Diagram.

- The Synchronization from Class Diagram to Entity Relationship Diagram dialog will be shown. You can synchronize multiple class diagram at once. The name of the target ERD can be specified directly in the dialog. You can also preview the original class diagram by clicking on the name of class diagram.

- By pressing OK, the Synchronize to Entity Relationship Diagram dialog will be shown. You can specify the Primary Key column, also the name of the mapping entity also the columns directly in this dialog.

- Now the Entity Relationship Diagram for your class model is being generated.

- Select Tools > Object-Relationship Mapping (ORM) > Generate Code to open the Database Code Generation dialog.

- In the Database Code Generation dialog, make sure you have selected generate Code and Database, also generate code for PHP.

- Specify the document root of your apache server in the Output Path field, and select Doctrine in Framework combo box.

- Switch to Database tab, and select Export to database. This will help you to create the schema directly in your database.

-
Press OK to proceed, and now the PHP ORM source code also the database schema is being generated.
Once the PHP ORM source code being generated, we can then move on to develop database applications. Let's create 2 simple pages to try out how to save and load objects with the generated code.
- Create a blank file under document root, name it as testsave.php.

- Open testsave.php with a text editor.
- Enter PHP tags in the file, and then include bootstrap.php in your testsave.php.

- Now open the connection with your database.

- Let's create a Person object, and specify the name as Peter also the address as House 1.

- Assume Peter having 2 cars. Let's create 2 car objects, and associate them with Peter.

- Finally save them to database. We also add an echo statement indicate the process is done.
- Open the browser and go to testsave.php, the word done should be echo in the page.

- Open MySQL console to query the Person and Car tables, records for Peter and his car are saved.

- Now create a new PHP file for tryout loading the saved records. Name the file as testload.php.
- Specific person object can be loaded by supplying the ID.

- Once we loaded the person objects, we can access the associated car objects through the cars collection.

- Open the testload.php in browser; the person objects and his associated car will be shown.

- You can also create query to load the entire object collections from database.

Resources
- code_sample.zip
- PersonCarDB.vpp
One Moment Please
Thanks for rating this article.