【问题标题】:Symfony Doctrine entity overviewSymfony Doctrine 实体概述
【发布时间】:2019-08-30 14:30:12
【问题描述】:

是否可以获得项目中所有映射实体的报告?

我想要一个控制台命令或一个外部包,它可以向我显示所有映射实体的详细列表,以及有关字段、类型、约束等的每个细节。

类似这样的:

产品

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

快速了解包含许多实体的项目可能非常有用。

【问题讨论】:

标签: php symfony doctrine


【解决方案1】:

有帮助吗?

<?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       |             |
+---------------------+--------------+-----------------------+----------+-------------+

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
  • 2016-04-13
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-05
相关资源
最近更新 更多