/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $em = $this->getContainer()->get('bbapp')->getEntityManager(); $orphans = $em->getConnection()->executeQuery('SELECT c.uid, c.classname FROM content c LEFT JOIN content_has_subcontent sc ON sc.content_uid = c.uid LEFT JOIN page p ON p.contentset = c.uid LEFT JOIN media m ON m.content_uid = c.uid WHERE sc.content_uid IS NULL AND p.contentset IS NULL AND m.content_uid IS NULL')->fetchAll(); $contents_count = $em->getConnection()->executeQuery('SELECT count(*) FROM content')->fetch(\PDO::FETCH_NUM); $before_contents_count = $contents_count[0]; $output->writeln("\nBefore cleaning, content table contains {$before_contents_count} row(s)" . " (including " . count($orphans) . " potentials orphans).\n"); foreach ($orphans as $orphan) { try { $classname = AbstractContent::getFullClassname($orphan['classname']); $orphan_object = $em->find($classname, $orphan['uid']); $em->getRepository($classname)->deleteContent($orphan_object); } catch (ClassNotFoundException $e) { $uid = $orphan['uid']; $em->getConnection()->executeUpdate("DELETE FROM content_has_subcontent WHERE content_uid = '{$uid}' OR parent_uid = '{$uid}'"); $em->getConnection()->executeUpdate("DELETE FROM revision WHERE content_uid = '{$uid}'"); $em->getConnection()->executeUpdate("DELETE FROM content WHERE uid = '{$uid}'"); } } $em->flush(); $contents_count = $em->getConnection()->executeQuery('SELECT count(*) FROM content')->fetch(\PDO::FETCH_NUM); $rows_saved = $before_contents_count - $contents_count[0]; $output->writeln("After cleaning, content table contains {$contents_count['0']} row(s) ({$rows_saved} row(s) saved).\n"); }