Error using Doctrine installed via Poser

Asked

Viewed 126 times

0

I installed Doctrine via Poser:

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

When running unit tests, the file location ArrayCollection 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 believe the problem is related to the folder structure generated by Composer.

Is the folder structure that Poser set up for Doctrine wrong? How to fix?

I released the code on gitlab.

I’m using php 5.4.30.

  • I copied your Composer.json and installed the packages here, there was no error executing the phpunit command. Try to delete the vendor folder and the Composer.lock file and have it installed again.

  • tries to download the git I shared. When running your guidelines, the same error appeared. The problem occurs in the USER class qd tries to instantiate an arraycollecction

1 answer

1


You need to use the full class name, ie the FQCN (Fully Classified class name) of it, if you want to use it.

If you don’t, PHP will find that you are trying to load the class into the same namespace the current file is in (i.e., DoctrineNaPratica\Model)

You can do it two ways:

Import class into block use, right after the statement 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 from 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.