Error using doctrine installed via composer

0

I installed Doctrine via composer:

{
  "require": {
      "doctrine/common": "2.4.*",
      "doctrine/dbal": "2.4.*",
      "doctrine/orm": "2.4.*",
      "phpunit/phpunit": "3.7.*"
  }
}

When running unit tests, the location of the ArrayCollection file was not found by namespace .

./vendor/bin/phpunit

Error presented:

  

Fatal error: Class 'DoctrineNaPratica \ Model \ ArrayCollection' not found in /Users/israel/Sites/doctrine/src/DoctrineNaPratica/Model/User.php on line 183

     

Fatal error: Class 'DoctrineNaPratica \ Model \ ArrayCollection' not found in /Users/israel/Sites/doctrine/src/DoctrineNaPratica/Model/User.php on line 183

This line 183 has the following code:

public function __construct() 
{            
    $this->courseCollection = new ArrayCollection;
    $this->lessonCollection = new ArrayCollection;
    $this->profileCollection = new ArrayCollection;
    $this->enrollmentCollection = new ArrayCollection;
}

And the Collection is declared via annotations :

/**
 * @ORM\OneToMany(targetEntity="Course", mappedBy="teacher", cascade={"all"}, orphanRemoval=true, fetch="LAZY")
 * 
 * @var Doctrine\Common\Collections\Collection   
 */
 protected $courseCollection;

I think the problem is related to the folder structure generated by the composer.

Is the folder structure the composer set up for the doctrine wrong? How do I fix it?

I made the code available in gitlab .

I'm using php 5.4.30.

    
asked by anonymous 11.04.2015 / 02:49

1 answer

1

You need to use the fully qualified class name of the class, if you want to use it.

If you do not do this, PHP will find that you are trying to load the class into the same namespace where the current file is (that is, DoctrineNaPratica\Model )

You can do this in two ways:

Import the class into block use , shortly after the declaration namespace , of your class:

use Doctrine\Common\Collections\ArrayCollection;

... or point the FQCN of the class when instantiating it or when calling a static method of it:

$this->courseCollection     = new Doctrine\Common\Collections\ArrayCollection;
$this->lessonCollection     = new Doctrine\Common\Collections\ArrayCollection;
$this->profileCollection    = new Doctrine\Common\Collections\ArrayCollection;
$this->enrollmentCollection = new Doctrine\Common\Collections\ArrayCollection;
    
12.04.2015 / 00:07