Generate models from Database with webtools

Note

In this part, your project is already created.
If you do not have a mysql database on hand, you can download this one: messagerie.sql

Configuration

Check the database configuration with webtools at http://127.0.0.1:8090/Admin/:

Go to models part:

../../_images/models-part.png

Note

The configuration file is located in app/config/config.php

Change the configuration of the database to use the messagerie database:

  • Click on the Edit config file button
  • Select the database part
../../_images/update-config-db.png
  • Enter messagerie in the dbName field
  • Click on Test button to check the connection
  • Validate with the Save configuration button
../../_images/models-part-2.png

Generation

To generate all models, click on (Re-)create all models button.

../../_images/models-generated.png

Generate the models cache by clicking on Re-init all models cache button:

../../_images/cache-generated.png

That’s all!
The models are generated and operational.
You can now see datas.

Note

It is possible to generate models automatically when creating a project with the -m option for models and -b to specify the database:

Ubiquity new quick-start -a  -m -b=messagerie

Checking

Class diagram

Still in the models part, click on the models generation step:

../../_images/step-models-generation.png

Then click on Classes diagram button:

../../_images/access-class-diagram.png

You can see the class diagram, using the yUML API

../../_images/classes-diagram.png

Models meta-datas

To see the metadatas of a created model:

Select a model, and activate the Structure tab:

../../_images/structure-tab.png

You can also view the partial class diagram:

../../_images/settings-class-diagram.png

Models validation

The third tab gives information about object validation:

../../_images/validation-info.png

Note

The validation rules were generated automatically with the classes.
They are defined through @validator annotations on each member of a class and are stored in cache.

The Validate instances button is used to check the validity of the instances:

../../_images/validation-results.png

Note

It is normal that by default all instances in the database do not check all validation rules.

Generated classes

Generated classes are located in app/models folder, if the configuration of mvcNS.models has not been changed.

Note

If you want to know more about:

  • object/relational mapping rules, see the ORM part
  • data querying and persistence, see DAO part

The Settings class:

app/models/Settings.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
 namespace models;
     class Settings{
             /**
              * @id
              * @column("name"=>"id","nullable"=>false,"dbType"=>"int(11)")
              * @validator("id","constraints"=>array("autoinc"=>true))
             **/
             private $id;

             /**
              * @column("name"=>"name","nullable"=>true,"dbType"=>"varchar(45)")
              * @validator("length","constraints"=>array("max"=>45))
             **/
             private $name;

             /**
              * @oneToMany("mappedBy"=>"settings","className"=>"models\\Organizationsettings")
             **/
             private $organizationsettingss;
     }

Important

Any modification on the classes (code or annotations) requires the reset of the cache to be taken into account.

Ubiquity init-cache -t=models