Skip to content

Latest commit

 

History

History
167 lines (116 loc) · 4.21 KB

scratch.rst

File metadata and controls

167 lines (116 loc) · 4.21 KB

Models creation from scratch

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.

Creating a model

Consider the following model representing a user:

/_static/images/model/scratch/user-model.png

We will create it with devtools, in command prompt:

Ubiquity model user

/_static/images/model/scratch/create-model.png

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

Adding fields

Select the Add fields menu item:

  • Enter the field names separated by a comma:

/_static/images/model/scratch/field-types.png

The added fields:

/_static/images/model/scratch/fields-added.png

Generating the class

/_static/images/model/scratch/generate-class.png

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;
         }


Modifying existing models

Ubiquity model

Without parameters, if some models exist, the model command suggests their loading:

/_static/images/model/scratch/reload.png

The model to achieve is now the following:

/_static/images/model/scratch/group_users.png

Select the Add/switch to model menu option, and enter group

/_static/images/model/scratch/switch-to-group.png

Add:
  • primary key id in autoinc
  • the name field
  • The manyToMany relation with the User class :

/_static/images/model/scratch/manytomany-users.png