Skip to content

Instantly share code, notes, and snippets.

@erd0s
Last active November 24, 2016 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erd0s/725a3dfa8e961f28be3302a56895a149 to your computer and use it in GitHub Desktop.
Save erd0s/725a3dfa8e961f28be3302a56895a149 to your computer and use it in GitHub Desktop.
<?php
namespace Seatfrog\WebservicesBundle\Command;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
function getDepth(ArrayCollection $set, $currentItem, $currentDepth) {
$currentDepth++;
$foreignKeys = $currentItem->getForeignKeys();
if (sizeof($foreignKeys) == 0) {
return $currentDepth;
}
$depths = new ArrayCollection();
foreach ($foreignKeys as $foreignKey) {
// Get the foreign table name
$foreignName = $foreignKey->getForeignTableName();
$target = $set->filter(function($o) use ($foreignName) {
return $o->getName() == $foreignName;
});
$target = $target->first();
$depths->add(getDepth($set, $target, $currentDepth));
}
return max($depths->toArray());
};
function sorter($a, $b) {
return $a['depth'] > $b['depth'];
};
class GetDumpOrderCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName("seatfrog:dump-order");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get("doctrine.orm.entity_manager");
$metadatas = $em->getMetadataFactory()->getAllMetadata();
$tool = new SchemaTool($em);
$schema = $tool->getSchemaFromMetadata($metadatas);
$command = "mysqldump --xml -u root -pPUTYOURPASSWORDHEREPLZ -h 127.0.0.1 DATABASENAMEGOESHERE ";
$final = [];
// Find the max depth
foreach ($schema->getTables() as $table) {
$depth = getDepth(new ArrayCollection($schema->getTables()), $table, 0);
$final[] = [
'table' => $table,
'depth' => $depth,
];
}
uasort($final, function($a, $b) {
return $a['depth'] > $b['depth'];
});
foreach ($final as $item) {
$command .= $item['table']->getName()." ";
}
$output->writeln($command);
}
}
@z4c
Copy link

z4c commented Nov 24, 2016

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment