Generate models from Database with devtools

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 devtools console program:

Ubiquity config database
../../_images/check-config.png

Note

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

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

Ubiquity config:set --database.dbName=messagerie
../../_images/update-dbName.png

Generation

To generate all models, use the all-models command:

Ubiquity all-models
../../_images/db-created.png

That’s all!
The models are generated and operational.

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

Models meta-datas

To obtain the metadatas of all created models:

Ubiquity info:models

For a precise model:

Ubiquity info:models -m=Groupe
../../_images/model-metas.png

Models validation info

To obtain the validation rules for the model User:

Ubiquity info:validation -m=User
../../_images/infos-validation-user.png

On a particular member (email):

Ubiquity info:validation -m=User -f=email
../../_images/infos-validation-user-email.png

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 User class:

app/models/User.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
namespace models;
     class User{
             /**
              * @id
              * @column("name"=>"id","nullable"=>false,"dbType"=>"int(11)")
              * @validator("id","constraints"=>array("autoinc"=>true))
             **/
             private $id;

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

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

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

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

             /**
              * @column("name"=>"suspended","nullable"=>true,"dbType"=>"tinyint(1)")
              * @validator("isBool")
             **/
             private $suspended;

             /**
              * @manyToOne
              * @joinColumn("className"=>"models\\Organization","name"=>"idOrganization","nullable"=>false)
             **/
             private $organization;

             /**
              * @oneToMany("mappedBy"=>"user","className"=>"models\\Connection")
             **/
             private $connections;

             /**
              * @manyToMany("targetEntity"=>"models\\Groupe","inversedBy"=>"users")
              * @joinTable("name"=>"groupeusers")
             **/
             private $groupes;
     }

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

Querying

Classes are generated, and models cache also.
At this point, we can already query the database in console mode, to give an idea of the possibilities of the DAO part:

Classic queries

Getting all the groups:

Ubiquity dao getAll -r=Groupe
../../_images/get-all.png

With there organization:

Ubiquity dao getAll -r=Groupe -i=organization
../../_images/get-all-groupes-orga.png

A more complete query:
Search for groups with the word “list” in their email, displaying the name, email and organization of each group:

Ubiquity dao getAll -r=Groupe -c="email like '%liste%'" -f=email,name,organization -i=organization
../../_images/query-groupes-orga.png

Getting one User by id:

Ubiquity dao getOne -r=User -c="id=4"
../../_images/get-one-user.png

uQueries

UQueries are special in that they allow to set criteria on the values of the members of the associated objects:

Search for groups with a user named Shermans

Ubiquity dao uGetAll -r=Groupe -c="users.lastname='Shermans'" -i=users
../../_images/groupes-sherman.png

We can verify that Shermans belongs to the group Auditeurs

Ubiquity dao uGetAll -r=User -c="groupes.name='Auditeurs' and lastname='Shermans'" -i=groupes
../../_images/shermans-groupe.png

The same with a parameterized query:

Ubiquity dao uGetAll -r=User -c="groupes.name= ? and lastname= ?" -i=groupes -p=Auditeurs,Shermans