Bonjour,

Voila je rencontre un petit problème a la création d'une entité avec une relation (ManyToOne). J'utilise la commande dans le terminal "php bin/console make:entity.

Class name of the entity to create or update (e.g. TinyGnome):

Article

created: src/Entity/Article.php
created: src/Repository/ArticleRepository.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):

title

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

Field length [255]:

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

updated: src/Entity/Article.php

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

slug

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

Field length [255]:

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

updated: src/Entity/Article.php

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

content

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

text

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

updated: src/Entity/Article.php

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

category

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

relation

What class should this entity be related to?:

Category

Relation type? [ManyToOne, OneToMany, ManyToMany, OneToOne]:

ManyToOne

Is the Article.category property allowed to be null (nullable)? (yes/no) [yes]:

Do you want to add a new property to Category so that you can access/update Article objects from it - e.g. $category->getArticles()? (yes/no) [yes]:

A new property will also be added to the Category class so that you can access the related Article objects from it.

New field name inside Category [articles]:

updated: src/Entity/Article.php
updated: src/Entity/Category.php

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

Success!

Puis je fais "php bin/console make:migration

Succes!

Puis quand je vais sur le fichier de migration j'ai ca :

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 Version20191124130213 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->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

    $this->addSql('CREATE TABLE article (id INT AUTO_INCREMENT NOT NULL, 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->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

    $this->addSql('DROP TABLE article');

    }
    }

Comme vous pouvez le voir dans la fonction up, j'ai que :

CREATE TABLE article (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB

Je comprends pas pourquoi j'ai pas les autres champs (title, slug, content et category).

Dans src/Entity/Article, j'ai bien tous les champs :

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**

  • @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
    */
    class Article
    {
    /**

    • @ORM\Id()
    • @ORM\GeneratedValue()
    • @ORM\Column(type="integer")
      */
      private $id;

    /**

    • @ORM\Column(type="string", length=255)
      */
      private $title;

    /**

    • @ORM\Column(type="string", length=255)
      */
      private $slug;

    /**

    • @ORM\Column(type="text")
      */
      private $content;

    /**

    • @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="articles")
      */
      private $category;

    public function getId(): ?int
    {
    return $this->id;
    }

    public function getTitle(): ?string
    {
    return $this->title;
    }

    public function setTitle(string $title): self
    {
    $this->title = $title;

    return $this;

    }

    public function getSlug(): ?string
    {
    return $this->slug;
    }

    public function setSlug(string $slug): self
    {
    $this->slug = $slug;

    return $this;

    }

    public function getContent(): ?string
    {
    return $this->content;
    }

    public function setContent(string $content): self
    {
    $this->content = $content;

    return $this;

    }

    public function getCategory(): ?Category
    {
    return $this->category;
    }

    public function setCategory(?Category $category): self
    {
    $this->category = $category;

    return $this;

    }
    }

Que faire svp ?

Merci de votre aide.

1 réponse


Bonjour,
Quel est votre environnement ? Prod ou dev ? En principe, les outils comme make viennent par défaut avec l'env de dev mais certaines personnes l'installent en prod. Dans ce dernier cas, il vous faudra vider le cache avant de générer votre migration.

Après, même si make:entity est trustable, mieux vaut toujours vérifier les classes générées ainsi que les annotations.
Pouvez-vous nous reposter les classes Category et Article bien formatées ?