Note
It is often preferable to design a database conceptually and then generate the models from the existing database.
The creation of models from scratch is only suitable for simple cases, and does not allow to skip a conceptualization phase.
Consider the following model representing a user:
We will create it with devtools, in command prompt:
Ubiquity model user
Note
A primary key is automatically added at creation as an auto-increment.
It is possible to change the default name of the primary key when launching the command :
Ubiquity model user -k=uid
Select the Add fields
menu item:
- Enter the field names separated by a comma:
The added fields:
Below is the created model, without the accessors:
.. tabs:: .. tab:: Attributes .. code-block:: php :linenos: :caption: app/models/User.php namespace models; use Ubiquity\attributes\items\Table; use Ubiquity\attributes\items\Id; #[Table('user')] class User{ #[Id] #[Column(name: "id",dbType: "int(11)")] #[Validator(type: "id",constraints: ["autoinc"=>true])] private $id; #[Column(name: "firstname",dbType: "varchar(30)")] #[Validator(type: "length",constraints: ["max"=>30,"notNull"=>false])] private $firstname; #[Column(name: "lastname",dbType: "varchar(45)")] #[Validator(type: "length",constraints: ["max"=>45,"notNull"=>false])] private $lastname; #[Column(name: "email",dbType: "varchar(150)")] #[Validator(type: "email",constraints: ["notNull"=>true])] #[Validator(type: "length",constraints: ["max"=>150])] private $email; } .. tab:: Annotations .. code-block:: php :linenos: :caption: app/models/User.php namespace models; /** * @table("name"=>"user") */ class User{ /** * @id * @column("id","int(11)") * @validator("id",["autoinc"=>true]) */ private $id; /** * @column("firstname","varchar(30)") * @validator("length",["max"=>30,"notNull"=>false]) */ private $firstname; /** * @column("lastname","varchar(45)") * @validator("length",["max"=>45,"notNull"=>false]) */ private $lastname; /** * @column("firstname","varchar(150)") * @validator("email",["notNull"=>false]) * @validator("length",["max"=>150]) */ private $email; }
Ubiquity model
Without parameters, if some models exist, the model
command suggests their loading:
The model to achieve is now the following:
Select the Add/switch to model
menu option, and enter group
- Add:
- primary key
id
in autoinc - the
name
field - The
manyToMany
relation with theUser
class :
- primary key