| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace AppBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader as DataFixturesLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\Bundle\DoctrineBundle\Command\DoctrineCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Doctrine\Common\DataFixtures\Executor\ORMExecutor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Doctrine\Common\DataFixtures\FixtureInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use AppBundle\Entity\Internal\Fixture; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class FixturesCommand extends DoctrineCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             ->setName('app:fixtures') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             ->setDescription('Load data fixtures to your database.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             ->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             ->setHelp(<<<EOT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | The <info>%command.name%</info> command loads data fixtures from your bundles: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | It loads only ones which were not appended already. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | <info>php %command.full_name%</info> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | <info>php %command.full_name% --env=prod</info> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | EOT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $doctrine = $this->getContainer()->get('doctrine'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $em = $doctrine->getManager($input->getOption('em')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $alreadyLoaded = $em->getRepository("AppBundle:Internal\Fixture")->findAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         // may support more managers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $loaded = $this->loadFixtures($output, $em, $alreadyLoaded); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         foreach ($loaded as $fixture) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $added = new Fixture(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $added->setName(get_class($fixture)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $em->persist($added); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $em->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |     private function loadFixtures(OutputInterface $output, EntityManager $em, array $loaded) | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         $logger = function($message) use ($output) { | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |             $output->writeln(sprintf('  <comment>></comment> <info>%s</info>', $message)); | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         }; | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |         $executor = new ORMExecutor($em); | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         $paths = []; | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |             $paths[] = $bundle->getPath() . '/Fixture'; | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         $loader = new DataFixturesLoader($this->getContainer()); | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         foreach ($paths as $path) { | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |             if (is_dir($path)) { | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |                 $loader->loadFromDirectory($path); | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |         $env = $this->getContainer()->getParameter('kernel.environment'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |         $output->writeln("Loading <comment>fixtures</comment>..."); | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         $has = array_map(function(Fixture $fixture) { | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |             return $fixture->getName(); | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |         }, $loaded); | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |         $fixtures = array_filter($loader->getFixtures(), function(FixtureInterface $fixture) use($has) { | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |             return !in_array(get_class($fixture), $has); | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |         if (!$fixtures) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |             $output->writeln("  Could not find any new <comment>fixtures</comment> to load.."); | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |             return []; | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |         $executor->setLogger($logger); | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         $executor->execute($fixtures, true); | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |         return $fixtures; | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                        
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: