简体   繁体   中英

Use Doctrine ArrayCollection in symfony custom command

I would like to use ArrayCollection in a custom symfony command.

But my way is throwing an error

The class 'Doctrine\\Common\\Collections\\ArrayCollection' was not found in the chain configured namespaces AppBundle\\Entity

My command

<?php

namespace AppBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity\Gameworld;

class MyCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('get:gw')
            ->setDescription('Populate gameworld')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getContainer()->get('doctrine')->getManager();

        $output->writeln('<info>Get current Gameworlds</info>');
        $gws = $em->getRepository('AppBundle:Gameworld')->findAll();

        $gws = new ArrayCollection($gws);

        /* rest of the command */
    }
}

I tried to do new \\Doctrine\\Common\\Collections\\ArrayCollection($gws);

But the error is still there.

Hope you may help.

You probably tries to create instance of ArrayCollection in Gameworld constructor(or somewhere in related entities constructors), but you did not imported it with use Doctrine\\Common\\Collections\\ArrayCollection; call. Try to check your AppBundle\\Entity\\Gameworld class and its related entities for occurances of ArrayCollection and check whether you import it properly inside that classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM