简体   繁体   English

在symfony自定义命令中使用Doctrine ArrayCollection

[英]Use Doctrine ArrayCollection in symfony custom command

I would like to use ArrayCollection in a custom symfony command. 我想在自定义symfony命令中使用ArrayCollection

But my way is throwing an error 但是我的方法是抛出一个错误

The class 'Doctrine\\Common\\Collections\\ArrayCollection' was not found in the chain configured namespaces AppBundle\\Entity 在链配置的名称空间AppBundle \\ Entity中找不到类'Doctrine \\ Common \\ Collections \\ ArrayCollection'

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); 我试图做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; 您可能试图在Gameworld构造函数(或相关实体构造函数中的某个位置)中创建ArrayCollection的实例,但没有使用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. 尝试检查AppBundle \\ Entity \\ Gameworld类及其相关实体是否存在ArrayCollection,并检查是否将其正确导入到该类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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