0

I created a fixtures for two related entities. When trying to load the fixtures with

php app/console doctrine:fixtures:load

I always get this error:

Fatal error: Class '[MyNamespace]\Entity\ArrayCollection' not found in [...]

It references this bit of code in my entity:

public function __construct()
    {
        $this->products = new ArrayCollection();
    }

When I uncomment the line in the function, all database records and their relations are created fine in my DB (MySQL).

What is going wrong here and what can be done to fix this?

TIA Matt

Additional information:

Entity "Provider"
<?php

namespace VS\Journeyro\WebBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Provider
 */
class Provider
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    private $slug;

    /**
     * @var string
     */
    private $url;

    /**
     * @var string
     */
    private $email;

    /**
     * @var string
     */
    private $phone;

    /**
     * @var string
     */
    private $image;

    /**
     * @var string
     */
    private $description;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Provider
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set slug
     *
     * @param string $slug
     * @return Provider
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

    /**
     * Get slug
     *
     * @return string 
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * Set url
     *
     * @param string $url
     * @return Provider
     */
    public function setUrl($url)
    {
        $this->url = $url;

        return $this;
    }

    /**
     * Get url
     *
     * @return string 
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return Provider
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set phone
     *
     * @param string $phone
     * @return Provider
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * Get phone
     *
     * @return string 
     */
    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * Set image
     *
     * @param string $image
     * @return Provider
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string 
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Provider
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    public function __toString()
    {
        return $this->getName();
    }


    /**
     * Add collection of related products
     */
    protected $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

    /**
     * Add products
     *
     * @param \VS\Journeyro\WebBundle\Entity\Product $products
     * @return Provider
     */
    public function addProduct(\VS\Journeyro\WebBundle\Entity\Product $products)
    {
        $this->products[] = $products;

        return $this;
    }

    /**
     * Remove products
     *
     * @param \VS\Journeyro\WebBundle\Entity\Product $products
     */
    public function removeProduct(\VS\Journeyro\WebBundle\Entity\Product $products)
    {
        $this->products->removeElement($products);
    }

    /**
     * Get products
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getProducts()
    {
        return $this->products;
    }
}

Class writing the fixtures for entity Provider
<?php

namespace VS\Journeyro\WebBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Collections\ArrayCollection;
use VS\Journeyro\WebBundle\Entity\Provider;

class LoadProviderData extends AbstractFixture implements OrderedFixtureInterface
{
    /**
     * {@inheritDoc}
     */

public function load(ObjectManager $manager)
    {

        $providers = array();

        // defining fixtures in $providers here

        foreach ($providers as $record) {
            $provider= new Provider();
            $provider->setName($record['name']);
            $provider->setSlug($record['slug']);
            $provider->setUrl($record['url']);
            $provider->setEmail($record['email']);
            $provider->setImage($record['image']);
            $provider->setDescription($record['description']);

            $manager->persist($provider);
            $manager->flush();

            $this->addReference('provider-'.$record['slug'], $provider);
        }


    }

/**
     * {@inheritDoc}
     */
    public function getOrder()
    {
        return 1;
    }
}
matt_jay
  • 1,241
  • 1
  • 15
  • 33
  • 1
    are you using the use statement ? like that : "use Doctrine\Common\Collections\ArrayCollection;" – Freelancer Jul 28 '14 at 15:19
  • 1
    I was not. I just added it the way you suggested, but the problem persists. My IDE does not appear to recognize the path however. I am finding what appears to be the relevant calls in this location: `vendor\doctrine\collections\lib\Doctrine\Common\Collections\ArrayCollection.php` – matt_jay Jul 28 '14 at 17:26
  • can you provide all the code in your class ? – Freelancer Jul 28 '14 at 17:33
  • added the entity and the class writing the fixtures in question to my original post. Is that what you were asking for? – matt_jay Jul 28 '14 at 18:05
  • 2
    It doesn't look like you've added your `use Doctrine\Common\Collections\ArrayCollection;` statement to the correct place: This should be at the top of the `Provider` class, as you reference the class name in the constructor. Try adding it there, rather than in your fixture. – ChrisC Jul 28 '14 at 18:11
  • As @ChrisC said you have to add the use statement in your entity Provider – Freelancer Jul 28 '14 at 19:02
  • Yup, that did it. Thanks, obviously I am still at the beginning ... – matt_jay Jul 28 '14 at 20:14
  • @codejak please validate the answer – Freelancer Jul 29 '14 at 07:55

1 Answers1

4

You have to put the following use statement in your Provider entity:

use Doctrine\Common\Collections\ArrayCollection;
Freelancer
  • 4,459
  • 2
  • 22
  • 28