Home > Software design >  Override symfony make:entity to load custom columns
Override symfony make:entity to load custom columns

Time:01-19

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

CodePudding user response:

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
<?= "<?php\n" ?>

namespace <?= $namespace ?>;

<?= $use_statements; ?>

#[ORM\Entity(repositoryClass: <?= $repository_class_name ?>::class)]
<?php if ($should_escape_table_name): ?>#[ORM\Table(name: '`<?= $table_name ?>`')]
<?php endif ?>
<?php if ($api_resource): ?>
#[ApiResource]
<?php endif ?>
<?php if ($broadcast): ?>
#[Broadcast]
<?php endif ?>
class <?= $class_name."\n" ?>
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    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.

CodePudding user response:

I would suggest to use an abstract class and extend it in your entities.

Example:

abstract class AbstractEntity {
   // define all shared properties

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

class MyEntity extends AbstractEntity {
   ...
}

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

  • Related