=========================================== .. 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 </model/messagerie.sql>
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
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
To obtain the metadatas of all created models:
Ubiquity info:models
For a precise model:
Ubiquity info:models -m=Groupe
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 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</model/models>
- data querying and persistence, see
DAO part</model/dao>
The User class:
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
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</model/dao>
:
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 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