-
-
Save z4c/395e3593c9d0b654d591a6935a1c0e6a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment