Ceci est une ancienne révision du document !


Symfony et les Entities (Entity)

Avant de jouer avec les Entities il faut que la base de données soit paramétrée et créée.

Création d'une Entity

Le but ici est de créé une entity avec les champs suivants:

  • Name : string
  • Url : string
  • VMaj : integer
  • VMin: integer
  • Revision : integer
  • Construction : integer
  • Online_Date : Date

make:entity

Avec la console php bin\console make:entity

D:\webprojects\test-api-project>php bin\console make:entity

 Class name of the entity to create or update (e.g. BraveGnome):
 > Projects 

 created: src/Entity/Projects.php
 created: src/Repository/ProjectsRepository.php
 
 Entity generated! Now let's add some fields!
 You can always add more fields later manually or by re-running this command.

 New property name (press <return> to stop adding fields):
 > name

 Field type (enter ? to see all types) [string]:
 > string

 Field length [255]:
 > 40

 Can this field be null in the database (nullable) (yes/no) [no]:
 > no

 updated: src/Entity/Projects.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > url

 Field type (enter ? to see all types) [string]:
 > string

 Field length [255]:
 > 

 Can this field be null in the database (nullable) (yes/no) [no]:
 > no

 updated: src/Entity/Projects.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > vmaj

 Field type (enter ? to see all types) [string]:
 > integer

 Can this field be null in the database (nullable) (yes/no) [no]:
 > 

 updated: src/Entity/Projects.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > vmin

 Field type (enter ? to see all types) [string]:
 > integer

 Can this field be null in the database (nullable) (yes/no) [no]:
 >

 updated: src/Entity/Projects.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > revision

 Field type (enter ? to see all types) [string]:
 > integer

 Can this field be null in the database (nullable) (yes/no) [no]:
 >

 updated: src/Entity/Projects.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > construction

 Field type (enter ? to see all types) [string]:
 > integer

 Can this field be null in the database (nullable) (yes/no) [no]:
 >

 updated: src/Entity/Projects.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > online_date

 Field type (enter ? to see all types) [string]:
 > date

 Can this field be null in the database (nullable) (yes/no) [no]:
 >

 updated: src/Entity/Projects.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 >


 
  Success! 
 

 Next: When you're ready, create a migration with make:migration

Types de champs possibles

Main types

  • string
  • text
  • boolean
  • integer (or smallint, bigint)
  • float

Relationships / Associations

  • relation (a wizard will help you build the relation)
  • ManyToOne
  • OneToMany
  • ManyToMany
  • OneToOne

Array/Object Types

  • array (or simple_array)
  • json
  • object
  • binary
  • blob

Date/Time Types

  • datetime (or datetime_immutable)
  • datetimetz (or datetimetz_immutable)
  • date (or date_immutable)
  • time (or time_immutable)
  • dateinterval

Other Types

  • json_array
  • decimal
  • guid

make:migration

D:\webprojects\test-api-project>php bin/console make:migration

  Success!


 Next: Review the new migration "src/Migrations/Version20191027131103.php"
 Then: Run the migration with php bin/console doctrine:migrations:migrate
 See https://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html

Voir : https://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html

doctrine:migrations:migrate

D:\webprojects\test-api-project>php bin/console doctrine:migrations:migrate

                    Application Migrations


WARNING! You are about to execute a database migration that could result in schema changes and data loss. 
Are you sure you wish to continue? (y/n)y
Migrating up to 20191027131103 from 0

  ++ migrating 20191027131103

     -> CREATE TABLE projects (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(40) NOT NULL, url VARCHAR(255) NOT NULL, vmaj INT NOT NULL, vmin INT NOT NULL, revision INT NOT NULL, construction INT NOT NULL, online_date DATE NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB

  ++ migrated (took 557.6ms, used 16M memory)

  ------------------------

  ++ finished in 569.9ms
  ++ used 16M memory
  ++ 1 migrations executed
  ++ 1 sql queries

Résultats de la migration

Vous pourriez laisser un commentaire si vous étiez connecté.