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

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

Generation¶
To generate all models, use the all-models command:
Ubiquity all-models

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

Models validation info¶
To obtain the validation rules for the model User:
Ubiquity info:validation -m=User

On a particular member (email):
Ubiquity info:validation -m=User -f=email

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:
The User class:
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 | namespace models;
use Ubiquity\attributes\items\Id;
use Ubiquity\attributes\items\Column;
use Ubiquity\attributes\items\Validator;
use Ubiquity\attributes\items\ManyToOne;
use Ubiquity\attributes\items\JoinColumn;
use Ubiquity\attributes\items\OneToMany;
use Ubiquity\attributes\items\ManyToMany;
use Ubiquity\attributes\items\JoinTable;
class User{
#[Id]
#[Column(name: 'id', nullable: false, dbType: 'int(11)')]
#[Validator('id', constraints: ['autoinc'=>true])]
private $id;
#[Column(name: 'firstname, nullable: false, dbType: 'varchar(65)')]
#[Validator('length', constraints: ['max'=>65, 'notNull'=>true])]
private $firstname;
#[Column(name: 'lastname, nullable: false, dbType: 'varchar(65)')]
#[Validator('length', constraints: ['max'=>65, 'notNull'=>true])]
private $lastname;
#[Column(name: 'email', nullable: false, dbType: 'varchar(255)')]
#[Validator('email', constraints: ['notNull'=>true])]
#[Validator('length', constraints: ['max'=>255])]
private $email;
#[Column(name: 'password', nullable: true, dbType: 'varchar(255)')]
#[Validator('length', constraints: ['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;
}
|
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 58 | 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

With there organization:
Ubiquity dao getAll -r=Groupe -i=organization

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

Getting one User by id:
Ubiquity dao getOne -r=User -c="id=4"

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

We can verify that Shermans belongs to the group Auditeurs
Ubiquity dao uGetAll -r=User -c="groupes.name='Auditeurs' and lastname='Shermans'" -i=groupes

The same with a parameterized query:
Ubiquity dao uGetAll -r=User -c="groupes.name= ? and lastname= ?" -i=groupes -p=Auditeurs,Shermans