0

Is it possible to get a report of all the mapped entities in a project?

I would like a console command or an external bundle which could show me the detailed list of all the mapped entities, along with every detail about fields, types, constraints and so on.

Something like this:

Product

|-------|-------------|--------|----------|-------------|
| Field | Type        | Column | Nullable | Constraints |
|-------|-------------|--------|----------|-------------|
| name  | string(255) | name   | no       | NotBlank    |
|-------|-------------|--------|----------|-------------|
| price | float       | price  | yes      | GreaterThan |
|-------|-------------|--------|----------|-------------|

It could be very useful to get a rapid overview of a project with many entities.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
StockBreak
  • 2,857
  • 1
  • 35
  • 61
  • what do you mean could explain more – Shkar Sardar Aug 30 '19 at 14:33
  • Try this https://stackoverflow.com/questions/15031534/get-array-list-of-entities-from-doctrine – nomistic Aug 30 '19 at 14:45
  • also https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/tools.html – nomistic Aug 30 '19 at 14:47
  • I added more details: `I would like a console command or an external bundle which could show me the detailed list of all the mapped entities, along with every detail about fields, types, constraints and so on.` – StockBreak Aug 30 '19 at 14:53
  • You can create your own console commands. If you use the link above, you can put something like that into a command: https://code.tutsplus.com/tutorials/how-to-create-custom-cli-commands-using-the-symfony-console-component--cms-31274 – nomistic Aug 30 '19 at 16:54
  • Here's a more detailed explanation https://abendstille.at/blog/?p=163 – nomistic Aug 30 '19 at 16:55

1 Answers1

3

Can it help?

<?php

    namespace App\Command;

    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    use Doctrine\Common\Persistence\ObjectManager;
    use Symfony\Component\Console\Helper\Table;

    class EntitylistCommand extends Command
    {

        protected static $defaultName = 'EntitylistCommand';

        protected function configure()
        {
            $this
                    ->setDescription('EntitylistCommand')
                    ->setHelp('EntitylistCommand');
        }
        public function __construct(ObjectManager $em)
        {
            $this->em = $em;

            // you *must* call the parent constructor
            parent::__construct();
        }
        protected function execute(InputInterface $input, OutputInterface $output)
        {

            /* @var $em \Doctrine\ORM\EntityManager */
            $em = $this->em;
            $tables = $em->getMetadataFactory()->getAllMetadata();
            foreach ($tables as $table) {

                $tablename = $table->getName();
                echo $tablename . PHP_EOL;

                $metadata = $em->getClassMetadata($tablename);
                $fields = $metadata->getFieldNames();
                $rows = array();
                foreach ($fields as $field) {

                    $fieldinfo = $metadata->fieldMappings[$metadata->getFieldName($field)];
                    $fieldname = $fieldinfo["fieldName"];
                    $fieldcolumnname = $fieldinfo["columnName"];
                    $fieldnullable = (isset($fieldinfo["nullable"]) ? ($fieldinfo["nullable"] ? "yes" : "no") : "no");
                    $fieldlength = (isset($fieldinfo["length"]) ? " (" . $fieldinfo["length"] . ")" : "");
                    $fieldtype = (isset($fieldinfo["type"]) ? $fieldinfo["type"] : "");

                    $rows[] = array($fieldname, $fieldtype . $fieldlength, $fieldcolumnname, $fieldnullable);
                }

                $table = new Table($output);
                $table
                        ->setHeaders(['Field', 'Type', 'Column', 'Nullable', 'Constraints'])
                        ->setRows($rows)
                ;
                $table->render();
            }
        }
    }
FOS\UserBundle\Model\User
+---------------------+--------------+-----------------------+----------+-------------+
| Field               | Type         | Column                | Nullable | Constraints |
+---------------------+--------------+-----------------------+----------+-------------+
| username            | string (180) | username              | no       |             |
| usernameCanonical   | string (180) | username_canonical    | no       |             |
| email               | string (180) | email                 | no       |             |
| emailCanonical      | string (180) | email_canonical       | no       |             |
| enabled             | boolean      | enabled               | no       |             |
| salt                | string       | salt                  | yes      |             |
| password            | string       | password              | no       |             |
| lastLogin           | datetime     | last_login            | yes      |             |
| confirmationToken   | string (180) | confirmation_token    | yes      |             |
| passwordRequestedAt | datetime     | password_requested_at | yes      |             |
| roles               | array        | roles                 | no       |             |
+---------------------+--------------+-----------------------+----------+-------------+
Manzolo
  • 1,909
  • 12
  • 13
  • 1
    Constraint extract example https://stackoverflow.com/questions/17184200/extract-constraints-form-doctrine-2-entity-in-symfony-2 – Manzolo Aug 30 '19 at 18:41
  • Very nice example, thank you! There should be something like this in the Symfony Doctrine Bridge. – StockBreak Sep 02 '19 at 09:22