Skip to content

Commit

Permalink
Post table ok
Browse files Browse the repository at this point in the history
  • Loading branch information
WebDevCF2m committed Sep 10, 2024
1 parent 363f712 commit 1d801bd
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 5 deletions.
132 changes: 131 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,134 @@ Avec l'id comme seul attribut.

Si on souhaite le remplir :

php bin/console make:entity Post
php bin/console make:entity Post

Puis les champs souhaités :

```bash
$ php bin/console make:entity Post
Your entity already exists! So let's add some new fields!
New property name (press <return> to stop adding fields):
> postTitle
Field type (enter ? to see all types) [string]:
>
Field length [255]:
> 160
Can this field be null in the database (nullable) (yes/no) [no]:
>
updated: src/Entity/Post.php
Add another property? Enter the property name (or press <return> to stop adding fields):
> postText
Field type (enter ? to see all types) [string]:
> text
text
Can this field be null in the database (nullable) (yes/no) [no]:
>
updated: src/Entity/Post.php
Add another property? Enter the property name (or press <return> to stop adding fields):
> postDateCreated
Field type (enter ? to see all types) [string]:
> datetime
datetime
Can this field be null in the database (nullable) (yes/no) [no]:
> yes
updated: src/Entity/Post.php
Add another property? Enter the property name (or press <return> to stop adding fields):
> postDatePublished
Field type (enter ? to see all types) [string]:
> datetime
datetime
Can this field be null in the database (nullable) (yes/no) [no]:
> yes
updated: src/Entity/Post.php
New property name (press <return> to stop adding fields):
> postPublished
Field type (enter ? to see all types) [string]:
> boolean
boolean
Can this field be null in the database (nullable) (yes/no) [no]:
> yes
updated: src/Entity/Post.php
```
### Première migration
php bin/console make:migration
Un fichier est créé dans le dossier `migrations`, on peut vérifier les requêtes SQL qui seront effectuées, nous acceptons pour voir le résultat :
php bin/console doctrine:migrations:migrate
### Modification de `src/Entity/Post.php`
```php
### ....
#[ORM\Entity(repositoryClass: PostRepository::class)]
class Post
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(
# non signé
options: [
'unsigned' => true,
]
)]
private ?int $id = null;
#[ORM\Column(length: 160)]
private ?string $postTitle = null;
#[ORM\Column(type: Types::TEXT)]
private ?string $postText = null;
#[ORM\Column(
type: Types::DATETIME_MUTABLE,
# valeur par défaut
options: [
'default'=> 'CURRENT_TIMESTAMP',
]
)]
private ?\DateTimeInterface $postDateCreated = null;
#[ORM\Column(
type: Types::DATETIME_MUTABLE,
nullable: true
)]
private ?\DateTimeInterface $postDatePublished = null;
#[ORM\Column(
options: [
# Par défaut, false (0)
'default'=> false,
]
)]
private ?bool $postPublished = null;
### ...
```
On doit refaire un `php bin/console make:migration puis une php bin/console d:m:m`
33 changes: 33 additions & 0 deletions migrations/Version20240910122417.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240910122417 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE post (id INT AUTO_INCREMENT NOT NULL, post_title VARCHAR(160) NOT NULL, post_text LONGTEXT NOT NULL, post_date_created DATETIME DEFAULT NULL, post_date_published DATETIME DEFAULT NULL, post_published TINYINT(1) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE messenger_messages (id BIGINT AUTO_INCREMENT NOT NULL, body LONGTEXT NOT NULL, headers LONGTEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', available_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', delivered_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_75EA56E0FB7336F0 (queue_name), INDEX IDX_75EA56E0E3BD61CE (available_at), INDEX IDX_75EA56E016BA31DB (delivered_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE post');
$this->addSql('DROP TABLE messenger_messages');
}
}
31 changes: 31 additions & 0 deletions migrations/Version20240910123736.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240910123736 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE post CHANGE id id INT UNSIGNED AUTO_INCREMENT NOT NULL, CHANGE post_date_created post_date_created DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, CHANGE post_published post_published TINYINT(1) DEFAULT 0 NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE post CHANGE id id INT AUTO_INCREMENT NOT NULL, CHANGE post_date_created post_date_created DATETIME DEFAULT NULL, CHANGE post_published post_published TINYINT(1) DEFAULT NULL');
}
}
27 changes: 23 additions & 4 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ class Post
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[ORM\Column(
# non signé
options: [
'unsigned' => true,
]
)]
private ?int $id = null;

#[ORM\Column(length: 160)]
Expand All @@ -20,13 +25,27 @@ class Post
#[ORM\Column(type: Types::TEXT)]
private ?string $postText = null;

#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
#[ORM\Column(
type: Types::DATETIME_MUTABLE,
# valeur par défaut
options: [
'default'=> 'CURRENT_TIMESTAMP',
]
)]
private ?\DateTimeInterface $postDateCreated = null;

#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
#[ORM\Column(
type: Types::DATETIME_MUTABLE,
nullable: true
)]
private ?\DateTimeInterface $postDatePublished = null;

#[ORM\Column(nullable: true)]
#[ORM\Column(
options: [
# Par défaut, false (0)
'default'=> false,
]
)]
private ?bool $postPublished = null;

public function getId(): ?int
Expand Down

0 comments on commit 1d801bd

Please sign in to comment.