0

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.

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
Théo Attali
  • 2,200
  • 1
  • 9
  • 10
  • Add a stack trace to the question, please. – Pieter van den Ham Sep 10 '17 at 15:04
  • Have you looked at [Relationship Mapping Metadata](https://symfony.com/doc/current/doctrine/associations.html#relationship-mapping-metadata)? Normally `ArrayCollection` is used in Entities for mapping. Your error says it's in the Entity file not the command file. – Alvin Bunk Sep 10 '17 at 15:24
  • @Pete How can i give you that ? – Théo Attali Sep 10 '17 at 16:16
  • @AlvinBunk `ArrayCollection` can also be used in controllers so maybe in custom commands too – Théo Attali Sep 10 '17 at 16:17
  • `throw $gws = new ArrayCollection();` return `[Symfony\Component\Debug\Exception\FatalThrowableError] Cannot throw objects that do not implement Throwable` – Théo Attali Sep 10 '17 at 16:23
  • You don't understand what ArrayCollection is used for, also you can't pass a parameter to it when constructing a new object. Instead use `new ArrayCollection()`. Please [read this so question](https://stackoverflow.com/questions/29180651/arraycollection-in-symfony) to understand how it is used. In that answer,@AlexandruOlaru give a good example of how to use it. – Alvin Bunk Sep 10 '17 at 17:35
  • I can use array collections in my commands – Sermanes Sep 11 '17 at 07:40

1 Answers1

1

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.

Valentin Knyazev
  • 156
  • 1
  • 11