skip to Main Content

for the current project that I am going to work on, I need to create many entities using the make:entity command.

Each time a new entity is created it needs to have the following columns that need to be defined

  • id_owner
  • sys_date_created
  • sys_date_modified
  • date_created
  • date_modified
  • id_group
  • id_user

The problem is that I will have to enter each of the following fields every time I create a new entity.

I have been looking at symfony and doctrine documentation if it is possible to override the make:entity function but to no avail.

What I would like is that each time we generate a new entity, the aboved mentioned fields are automatically generated.

For ex just the column id that is generated automatically by symfony.

If anyone has any working code or link to share that would be great.
Thanks in advance

2

Answers


  1. I took a look at the maker bundle’s Generator class and it turns out that there is a reasonable solution to override the templates being used.

    The generator’s code has:

           $templatePath = $templateName; // doctrine/Entity.tpl.php
            if (!file_exists($templatePath)) {
                $templatePath = __DIR__.'/Resources/skeleton/'.$templateName;
    
                if (!file_exists($templatePath)) {
                    throw new Exception(sprintf('Cannot find template "%s"', $templateName));
                }
            }
    

    So when looking for a template file the generator first looks in the current directory and then in the maker bundle’s Resources/skeleton directory. So in your applications main directory make a directory called doctrine, copy in the default template file and edit to suit. For example

    # project_dir/doctrine/Entity.tpl.php
    # Copy this from vendor/symfony/maker-bundle/Resources/skeleton/Entity.tpl.php
    <?= "<?phpn" ?>
    
    namespace <?= $namespace ?>;
    
    <?= $use_statements; ?>
    
    #[ORMEntity(repositoryClass: <?= $repository_class_name ?>::class)]
    <?php if ($should_escape_table_name): ?>#[ORMTable(name: '`<?= $table_name ?>`')]
    <?php endif ?>
    <?php if ($api_resource): ?>
    #[ApiResource]
    <?php endif ?>
    <?php if ($broadcast): ?>
    #[Broadcast]
    <?php endif ?>
    class <?= $class_name."n" ?>
    {
        #[ORMId]
        #[ORMGeneratedValue]
        #[ORMColumn]
        private ?int $id = null;
    
        public function getId(): ?int
        {
            return $this->id;
        }
        // Add your custom stuff here
        public function getWhatever() : void {}
    }
    

    After which the make:entity command will pick up your own custom template. It is not a perfect solution. If the maker bundle updates the template then you will need to update yours. Also it would be nice if you could tell the generator where to look but it does work.

    Login or Signup to reply.
  2. I would suggest to use an MappedSuperclass and extend it in your entities.

    Example:

    /** @MappedSuperclass */
    class Person {
       // define all shared properties
    
        /**
         * @ORMColumn(type="string", length=255)
         */
        private $someProperty;
    }
    
    
    /** @Entity */
    class Employee extends Person {
       ...
    }
    
    

    This still enables you to use the maker bundle to create entities. However, after creating it, don’t forget to extend the MappedSuperclass class.

    In the docs you can find some more details about inheritance mapping.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search