Reputation: 2895
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:
|-------|-------------|--------|----------|-------------|
| 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.
Upvotes: 0
Views: 173
Reputation: 1969
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 | |
+---------------------+--------------+-----------------------+----------+-------------+
Upvotes: 3